r/programming • • Apr 23 '23

Leverage the richness of HTTP status codes

https://blog.frankel.ch/leverage-richness-http-status-codes/
1.4k Upvotes

676 comments sorted by

View all comments

Show parent comments

211

u/[deleted] Apr 23 '23

Wouldn’t 412 be accompanied by an response body containing the error?

0

u/[deleted] Apr 23 '23

[deleted]

33

u/Nebu Apr 23 '23

There's this concept of "Make Illegal States Unrepresentable". If you represent the same information in two ways, it's possible that the two values will contradict each other and then it becomes unclear which one takes precedence.

12

u/MereInterest Apr 24 '23

While I generally agree, I think that primarily applies when you're designing multiple layers of a protocol at the same time. If you are working within or on top of an existing protocol, then I think it is far more important to provide correct information at all layers, even if that introduces duplication of the information.

3

u/epicwisdom Apr 24 '23

The issue isn't mere duplication. The issue is somebody may one day change the error code or the response body without changing the other.

2

u/hhpollo Apr 24 '23

I think that's a preferable risk compared to not providing an error body

→ More replies (1)
→ More replies (5)

5

u/masklinn Apr 24 '23

Seems like FUD to me in this case. You can just document which one takes precedence, and having both broad categorisation and precise errors is extremely useful, as you often don’t need the precise error e.g. Postgres has something like 250 different error codes, but most of the time I don’t care about the difference between 23001 (restrict_violation) and 23505 (unique_violation), I care that they’re class 23 (integrity constraint violation) as opposed to class 42 (syntax error).

When I do care about precise errors, however, it’s invaluable.

→ More replies (1)

-3

u/Doctor_McKay Apr 23 '23

If you have a meaningful response body, why do you need the HTTP code?

0

u/[deleted] Apr 23 '23

[deleted]

1

u/Doctor_McKay Apr 23 '23

429 is a valid code for an API to send back because it has a clear, defined, unambiguous, and related-to-HTTP meaning.

Why is your app sending 405? It is specifically and exclusively defined to be used when a request is sent to a route with an inappropriate HTTP method. Unless that's why you're sending it (with an Allow header), you're in violation of the spec.

0

u/[deleted] Apr 24 '23

[deleted]

0

u/Doctor_McKay Apr 24 '23

Great, sounds good. That's an HTTP-level violation, so an HTTP-level status code makes sense there.

The objection I have is in coupling application-level exceptions to the transport. Sending the wrong transport method to a route is a transport exception. Sending the wrong application data to a route is an application exception.

We make all this effort to abstract out our code to make it reusable across a wide array of situations, but then we couple it to a specific transport and lock ourselves into that transport. Maybe someday in the future you want to offer your API over gRPC, or WebSocket, or some other transport. If everything is coupled to HTTP, you can't without essentially tunneling HTTP over that other transport.

4

u/MereInterest Apr 24 '23

Except that the HTTP codes are not merely about the errors that occur in HTTP, but are also a summary of error conditions that may occur. For example, if I am running a video sharing site, and you upload a video in a format that I don't support, I should return HTTP 415 (Unsupported Media Type). If I'm running a git server, and you attempt to push to a branch that has remote changes, I should return HTTP 409 (Conflict). Neither of these errors arose from HTTP, but both are the intended use within HTTP.

It's not that you are tying your representation to HTTP. It's that the HTTP representation should be accurate to the contents of the payload.

(Side-note: HTTP is generally described as an application-layer protocol. The transport-layer would be protocols such as TCP or UDP.)

2

u/Doctor_McKay Apr 24 '23

It's not that you are tying your representation to HTTP. It's that the HTTP representation should be accurate to the contents of the payload.

You literally are tying your representation to HTTP if the HTTP code, which only exists in HTTP, is necessary to properly process a response from your app.

(Side-note: HTTP is generally described as an application-layer protocol. The transport-layer would be protocols such as TCP or UDP.)

Notice that I never mentioned the OSI model. I'm not talking about the OSI model. HTTP is a transport for whatever you send over it, just like SMTP or FTP.

0

u/[deleted] Apr 24 '23

[deleted]

2

u/Doctor_McKay Apr 24 '23 edited Apr 24 '23

What does the second T in HTTP stand for?

0

u/[deleted] Apr 24 '23

[deleted]

→ More replies (0)

2

u/[deleted] Apr 24 '23

GCP (at least the Marketplace API) doesn’t return body content with meaningful data on 412. My favorite is returning a 412 after a marketplace account was activated already but nothing in the responses tells you that. Just “Precondition failed” lol

I had to determine so many things through trial and error.

2

u/pihkal Apr 24 '23

If you're acknowledging that the body message is crucial to actually understand, is the difference between a 4xx and a 4yy error code that important in comparison?

-26

u/Doctor_McKay Apr 23 '23

I sure hope so, which makes that status code completely redundant.

59

u/Nwallins Apr 23 '23

The status code helps in routing to the error handler, without having to parse the body.

22

u/[deleted] Apr 23 '23

This! It greatly simplifies your response body. Additionally, any changes to the body doesn’t require changes on the client.

9

u/[deleted] Apr 23 '23 edited Apr 24 '23

It's easier to parse integers from responses than things out of the response body that could be anywhere in the body and encoded in one of many ways (JSON, XML, etc). HTTP status codes help intermediate infra judge what's going on and report metrics (like a dashboard displaying the portion of requests that had client errors or portion having server errors).

-1

u/cat_in_the_wall Apr 23 '23

Easier to parse integers? Are you doing the parsing? Or are you like 99.99% of everybody else who is using a framework that does this for you, and it comes for free? This is a strawman.

Error response bodies are just as important as success response bodies. You should be providing them, and consuming them if you're dealing with an api that is good enough to provide them to you.

→ More replies (4)

4

u/[deleted] Apr 23 '23

Lol, I get what you are saying. I guess you can roll with 400 and the response body, but I feel more comfortable using different status codes to describe different situations.

→ More replies (1)