Search code examples
resthttp-response-codes

RESTful response code for bad preexisting data?


I have a case where sometimes the data in our database is insufficient to process a request (ie, we need an account number or something that hasn't been populated).

If the user's request contained invalid syntax, it'd be a 400. If the database were down, or a service were failing, it'd be a 500, 502, or 503.

I'm considering 422, which basically means "the request was not malformed, but the semantics of your request have led to an error". The issue here is that it's not something inherent to the request itself, but rather to the state of our pre-existing data.


Solution

  • The behavior you detailed is a server error. Hence, 5xx response codes should be used instead of 4xx (which represent a client error). Now on to which response code in 5xx family; While there does not seem to exist any response code that defines your server behavior correctly, 500 Internal Server Error seems to be the closest one. Take a look at the rfc here: https://www.rfc-editor.org/rfc/rfc2616#section-10.5.1

    It also depends on which operation you are doing with your service and which resource.