'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!
Solution 1:[1]
The appropriate error code depends on the implementation and where the concurrency error is detected.
If you use Http If-Match header the Http specification requires a 412 precondition failed (error because of header information), while 409 is used if the entity would cause a conflict.
In a race condition two requests could pass the If-Match check and only the database or the domain layer detects the conflict. In that case I would return a 409, because the precondition was valid.
If you wouldn't use If-Match and ETags, but use a version id in the body then you need to use 409. Be aware that DELETE should not have a body.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
