I am looking for a correct status code to send for a gneral failure through an api.
The exact scenario is failing to add an product to a shopping cart.
The failure could happen for a large number of reasons, but i would like to return a single http code.
Which would be best?
I have been looking through them and cant see anything that exactly fits the needs here.
Some of the possible failure conditions could be:
Not enough stock to satisfy
Stock limit reached for that particular product
Product no longer available
A 444 No Response error indicates that the server has closed the connection with no returned information from a client request.
A generic error message, given when an unexpected condition was encountered and no more specific message is suitable. 501 Not Implemented. The server either does not recognize the request method, or it lacks the ability to fulfil the request.
Status codes 309 through 399 are currently unassigned.
The HTTP 402 Payment Required is a nonstandard response status code that is reserved for future use. This status code was created to enable digital cash or (micro) payment systems and would indicate that the requested content is not available until the client makes a payment.
If it's server error then it should be 500. If it's client error, use 400.
It's hard to be more precise than that without seeing the URI and what you do with it. For example, if "Product no longer available" is a result of GET request, then it should be 404 (not found). But if it was a POST request, then it should be 200 or 202.
For the other two, they might not be error. It could be the client has sent the correct request but the stock has been consumed by someone else, in this case server should return 409 (conflict) . If the request was for too much stock from the start, then it should just be 200/202.
If you had to have only one code, just use 400 and 200 (see above).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With