Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what status code to throw when there is a concurrency error?

I have a rest that receives among other things, a date and with it, makes a reservation. The problem occurs when 2 people "at the same time" try to book on the same day, at the same time.

Obviously, the first one who makes the request, will be able to book the appointment, so I will return a status of 200. On the other hand, the one that arrives later, will make the same request, but the server will throw an error because it can not reserve the Same appointment (already reserved). In this case, what http state code should be thrown?

A family code of 500 would not seem right, because the exception that is thrown, is caused by the very logic of the business.

On the other hand, a state code of the family of 400 would not seem right either because the request is well formulated

Thanks!

like image 702
Geronimo Avatar asked Oct 17 '22 08:10

Geronimo


1 Answers

I was researching this myself and jonrsharpe's suggestion of 409 Conflict is most appropriate.

https://www.rfc-editor.org/rfc/rfc7231#section-6.5.8:

The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict.

Conflicts are most likely to occur in response to a PUT request. For example, if versioning were being used and the representation being PUT included changes to a resource that conflict with those made by an earlier (third-party) request, the origin server might use a 409 response to indicate that it can't complete the request. In this case, the response representation would likely contain information useful for merging the differences based on the revision history.

like image 121
JohnOpincar Avatar answered Nov 15 '22 12:11

JohnOpincar