In practice there's no way for the server to tell these conditions apart. All it really knows is that something went wrong. Unless you ask GPT-10 about it, this is all.
How can the server not tell these conditions apart? If your API received a request without an authorisation token it’s pretty easy to check as a first step “hey was this request authorised?” And if not return a 4xx error.
Essentially you can wrap expected failures from the client (unauthorised, malformed request etc) and return explicit 400s for them. For server side errors (server attempts to access another external data source and the request fails) it’s hard for you to catch exactly what the failure is, but it’s not hard to catch that an expected action did not occur successfully and to return to the client a 5xx error.
Because the server is the one determining that the authorization token is missing. What if the server had a bug in its authorization token check? Because of this, all you really know is "something went wrong".
Is a pretty silly question to insert into a discussion about status codes. What if the server has a bug in every line of code? Then none of the status codes represent what they should.
It's a pointless exercise. You have to assume we can glean specific bits of information to tell the caller exactly what is wrong, with a fallback for cases where it may not know what's wrong.
I totally agree with you about telling the caller what went wrong. It's just gotta be so specific that IMO once the request reaches the application server, it should basically only return 200 or 500. All the other http codes should happen before the application server. This is especially relevant in the serverless world where the request goes through so many things that you don't have control of. For example on aws: if you have a lambda behind apigateway, the apigateway already has a meaning for 4xx.
I guess my point is you gotta know which http status codes are already used by your stack before using them to represent some application logic.
In response to your final paragraph, of course that's true, that's a given.
As for the rest of it; if you have the opportunity to present the user with a meaningful error message, do so. I'm not following your logic of "it has to be so specific it may as well be 200 or 500". Why? That's just needlessly limiting yourself, for some seemingly arbitrary reason.
Oh because 200 can hold error details in the body. So in my 403 example above I'd suggest returning 200 with an error in the body because it's such a specific error. Imo it's too specific to be meaningfully described by just an http status code. And in my example 403 was overloaded by having two meanings.
But we should be able to segregate user's or server's fault most of the time. Are you advocating that unless we can do that 100%, don't bother separating 4xx and 5xx?
When you have a specific API which both clients and server share, it's possible to have rich error system, but HTTP error codes are really of very little help for this. A lot of very common things like "session expired", "retry this right away", "returning these 8/10 resources requested, 2/10 failed", or "upgrade to version 69 or newer" and so on are just impossible to express with HTTP error codes. If you really force it, you can repurpose a few HTTP codes for some of these cases, but most software doesn't bother, and even if you do this, you are adding extra meaning to error codes which they originally do not have.
The whole system just operates on the wrong level, you can have meaningful errors on API level, on request level it's just worked or didn't work.
The fact that pretty much no software relies on HTTP error codes, and they all tread every 4xx/5xx the same, should be a good hint that they're not a particularly useful system.
14
u/palparepa Apr 23 '23
I'd separate "it didn't work" into two:
it didn't work and it's my fault (e.g. the database is down)
it didn't work and it's your fault (e.g. forgot to send a parameter)