Http for an error response due to a business exception. The syntax is correct and the query makes sense

There are many questions about the correct response status, but I couldn't figure out what status I should be using for a normal business exception. I have read the definition of 400 and it seems to me that it is due to communication errors.

The server cannot or will not process the request due to something that is perceived as a client error (for example, invalid request syntax, misshapen request messages, or a misleading routing request).

Suppose the client wants to confirm some operation. He sends me an absolutely correct, valid request, which I understand and process. But the confirmation code is incorrect. So this is a mistake, but this mistake is normal and expected, our communication is correct. Or another example: a client wants to withdraw money from an account. Again, the request is correct and valid, but there is not enough money in the account. I am currently using 400 , but 400 seems to me that I am wrong about communication between client and server, not about application logic. Maybe there is a more appropriate status for such errors? What are you using?

+3


source to share


1 answer


You can use HTTP 422 (Unprocessable Entity) for these cases. I prefer this rule of choosing http status codes:

  • HTTP 200, 201, 204 when the request was processed successfully
  • HTTP 422 when the request violates some business rule.
  • HTTP 500 when unexpected exceptions are thrown during processing


Resource: Nice Blog

+1


source







All Articles