▲ | lelanthran 2 days ago | ||||||||||||||||||||||||||||||||||||||||
> Oof, an HTTP 200 OK response with a body that says the request actually was not OK. What's "oof" about it? The application layer should not inject error codes into the transport layer which is what HTTP is in this case. Do you also think that Apache/Nginx should be injecting codes into IP packets? If your application injects codes into the HTTP layer, how on earth does the client know whether the error originated at the application or at the reverse proxy/webserver? | |||||||||||||||||||||||||||||||||||||||||
▲ | mtlynch 2 days ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||
>The application layer should not inject error codes into the transport layer which is what HTTP is in this case. I don't follow. What's the boundary between transport layer and application layer in a Go web app? The Go web app is responsible for specifying both the HTTP status code and the response body. What's the purpose of different HTTP 4xx errors if they're not supposed to come from the application? >Do you also think that Apache/Nginx should be injecting codes into IP packets? No, because Apache/Nginx is not responsible for populating IP datagrams, whereas Go web apps are responsible for generating the entire HTTP response. >If your application injects codes into the HTTP layer, how on earth does the client know whether the error originated at the application or at the reverse proxy/webserver? It can't. What design do you have in mind where a client gets a HTTP 404 and can distinguish between the web server and the application server? Are you saying a "not found" at the application layer should return HTTP 200 and the client has to check the HTTP body for the real error code? | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
▲ | imiric 2 days ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||
> The application layer should not inject error codes into the transport layer which is what HTTP is in this case. Huh? HTTP is an application layer protocol. It's perfectly acceptable for the application to return a non-200 status code when the request is invalid and can't be processed. There's a widely accepted status code for that exact scenario: 400 Bad Request. It informs the client that there was something wrong with their request, and in well-designed APIs, reading the response body would tell them the reason why. It would be wasteful for the client to always read the response and parse structured data to decide whether the request was successful (at the application level) or not. Status codes allow us to do that. That said, I've seen arguments for and against this practice, as sibling comments mention, and ultimately consistency and documentation are more important than semantics. The reason this line is blurry nowadays is because in the beginning web servers didn't contain complex logic. The web server was the application. Then came CGI scripts and application servers, and suddenly the application itself was making protocol-level decisions. The way this is typically structured in large applications is to have protocol-level abstractions that translate app-level errors into HTTP errors. But in small applications it's acceptable, though unsightly, to have HTTP logic mixed with business logic. > Do you also think that Apache/Nginx should be injecting codes into IP packets? Web servers do speak TCP/IP, so I'm not sure what your point is. Usually this is not something regular web apps need to be concerned with, but it's possible and sometimes desirable to introduce logic at the TCP or IP layer. There are proxy tools that work at both layer 4 and layer 7. > If your application injects codes into the HTTP layer, how on earth does the client know whether the error originated at the application or at the reverse proxy/webserver? By the status code, error message, and headers. An application would typically never return 502 Bad Gateway, a 301/302 redirect, or set headers like Cache-Control. By that same token, a reverse proxy/webserver would typically never override a 404 with a 200, or inject JSON error messages in the payload. The application ultimately decides the Content-Type of the response, which Content-Types it supports, and which headers it expects, so why shouldn't it also decide which status codes to return and which response headers to set? A gateway between it and the user can change or enhance this protocol, and specific gateways could be extracted to handle common things like authn/authz and load balancing, but the frontend gateway shouldn't override the message the application is sending (in typical circumstances). Both things can coexist with different responsibilities while speaking the same protocol. HTTP is flexible enough to support that. I'm curious, though: if you treat HTTP as the transport layer, what protocol does your application speak to the gateway? Is there some translation gateway that translates application-level semantics into HTTP ones? | |||||||||||||||||||||||||||||||||||||||||
|