News for hackers. Stuff that matters.
Re:Feed readers which don't take "no" for an answer by jannes on Sunday December 22, 2024 @08:25AM (#42486149)
The HTTP protocol is a lost art. These days people don't even look at the status code and expect some mumbo jumbo JSON payload explaining the error.
Re:Feed readers which don't take "no" for an answer by klntsky on Sunday December 22, 2024 @08:57AM (#42486341)
I would argue that HTTP statuses are a bad design decision, because they are intended to be consumed by apps, but are not app-specific. They are effectively a part of every API automatically without considerations whether they are needed.

People often implement error handling using constructs like regexp matching on status codes, while with domain-specified errors it would be obvious what exactly is the range of possible errors.

Moreover, when people do implement domain errors, they just have to write more code to handle two nested levels of branching.

Re:Feed readers which don't take "no" for an answer by throw0101b on Sunday December 22, 2024 @10:48AM (#42487040)
> I would argue that HTTP statuses are a bad design decision, because they are intended to be consumed by apps, but are not app-specific.

Perhaps put the app-specific part in the body of the reply. In the RFC they give a human specific reply to (presumably) be displayed in the browser:

   HTTP/1.1 429 Too Many Requests
   Content-Type: text/html
   Retry-After: 3600

   <html>
      <head>
         <title>Too Many Requests</title>
      </head>
      <body>
         <h1>Too Many Requests</h1>
         <p>I only allow 50 requests per hour to this Web site per
            logged in user.  Try again soon.</p>
      </body>
   </html>
* https://datatracker.ietf.org/doc/html/rfc6585#section-4

* https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429

But if the URL is specific to an API, you can document that you will/may give further debugging details (in text, JSON, XML, whatever).

Re:Feed readers which don't take "no" for an answer by marcosdumay on Sunday December 22, 2024 @10:42AM (#42486999)
> because they are intended to be consumed by apps, but are not app-specific

Well, good luck designing any standard app-independent protocol that works and doesn't do that.

And yes, you must handle two nested levels of branching. That's how it works.

The only improvement possible to make it clearer is having codes for API specific errors... what 400 and 500 aren't exactly. But then, that doesn't gain you much.

Re:Feed readers which don't take "no" for an answer by est on Monday December 23, 2024 @02:38AM (#42492588)
> error handling using constructs like regexp matching on status codes

Oh the horror. I would assume the practice is encourage by "RESTful" people?

Re:Feed readers which don't take "no" for an answer by KomoD on Sunday December 22, 2024 @09:47AM (#42486626)
That's because a lot of people refuse to use status codes properly, like just using 200 everywhere.
Re:Feed readers which don't take "no" for an answer by kstrauser on Sunday December 22, 2024 @11:37AM (#42487392)
A colleague who should’ve known better argued that a 404 response to an API call was confusing because we were, in fact, successfully returning a response to the client. We had a long talk about that afterward.
Re:Feed readers which don't take "no" for an answer by Joker_vD on Sunday December 22, 2024 @12:50PM (#42487909)
No, it is pretty confusing: the difference between 404 from hitting an endpoint that the server doesn't serve (because you forgot to expose this endpoint, oops!) and a 404 that means "we've successfully performed the search in our DB for the business entity you've requested and guarantee you that it does not exist" is rather difficult to tell programmatically.
Re:Feed readers which don't take "no" for an answer by yjftsjthsd-h on Sunday December 22, 2024 @04:04PM (#42489087)
I'm open to arguing about which error to return in each case, but surely we can agree that neither of those warrant a 200?
Re:Feed readers which don't take "no" for an answer by echoangle on Sunday December 22, 2024 @05:31PM (#42489732)
Why not? I wouldn’t say „I performed the search and there’s 0 results“ is an error condition. It’s just the result of a search, and everything went fine.
Re:Feed readers which don't take "no" for an answer by yjftsjthsd-h on Sunday December 22, 2024 @06:11PM (#42489966)
Hm, maybe? I guess it depends on what we mean by search; if myapp.com/search?someproduct finds that there are 0 matches then yeah that's probably a 200, but if myapp.com/products/123456 fails because no product has id 123456 then that's a textbook 404.
Re:Feed readers which don't take "no" for an answer by wruza on Sunday December 22, 2024 @08:50PM (#42490993)
It’s both nonsense, cause what you see here is a double conversion from an arbitrary problem domain into http domain and back again. Using a specific http code together with an app-domain code could make sense iff you wanted an intermediate host (a proxy etc) to perform some additional operation based on that status. Otherwise http status doesn’t speak the call language and can be just OK. (400/500 should still be handled by a client).

Back-and-forth conversion is a very poor idea. It works for what was “internet resources” initially (basically files and folders), but later people stretched that on application data models and that creates constant issues because people naturally can’t understand the mapping, cause there’s none. This is not a good idea. Talk to http hosts with http and talk to your client with a language you designed specifically for talking to it. 200 vs non-200 is http level and orthogonal to in-service statuses.

Re:Feed readers which don't take "no" for an answer by Joker_vD on Monday December 23, 2024 @05:07AM (#42493263)
No, the latter is absolutely a 200 because of separation of concerns and layering.

The HTTP server, when it detects "URI handler not found" condition, builds an 404 HTTP response and sends it as a normal payload through the underlying connection instead of turning it into an TLS error packet or an RST packet on TCP level (that's the TCP's standard response for "port handler process not found", after all) or something silly like that, and that is absolutely fine, because the application-level (HTTP) error messages should be transmitted by the transport level (TLS/TCP) just as normal messages would.

The same reasoning holds just the same when we consider the usage of HTTP as a transport-level protocol for some higher-level RPC exchange. Yes, HTTP has some assortment of error codes that superficially look like they can be reused to serve as the upper-layer errors as well but that's a red herring.

Re:Feed readers which don't take "no" for an answer by wiml on Sunday December 22, 2024 @05:28PM (#42489710)
If the URL identifies a resource (REST-style) and that database entry doesn't exist, then yes, 404 is less confusing response. If the URL identifies an API endpoint (RPC-style) then, sure, tunnel the error inside a "I successfully failed to handle that request" response if you like.
Re:Feed readers which don't take "no" for an answer by reshlo on Sunday December 22, 2024 @05:55PM (#42489892)
All URLs used when interacting with an API obviously identify API endpoints. There is no such thing as a URL which is part of an API but which is not an API endpoint.

There is a difference between /api/entity/123 and /api/search with a payload of 123, though.

Re:Feed readers which don't take "no" for an answer by tbrownaw on Sunday December 22, 2024 @11:30PM (#42491786)
422 unprocessable content (webdav)

The request couldn't be processed due to semantic errors... perhaps such as not being mapped to a handler :->

I suppose from the right point of view that could also be likened to a reverse proxy not being able to send the request on (502 bad gateway), but sane people would probably find that even more confusing.

There were also attempts to use 204 no content for "I successfully confirmed that what you asked for doesn't exist", but I think I managed to shoot those down.

Re:Feed readers which don't take "no" for an answer by AznHisoka on Sunday December 22, 2024 @09:37AM (#42486563)
I dont look at the code because its wrong sometimes. Some pages return a 200 yet display an error in the page
Re:Feed readers which don't take "no" for an answer by DaSHacka on Sunday December 22, 2024 @12:11PM (#42487651)
Nothing more annoying than a 200 response when the server 'successfully' serves a 404 page
Re:Feed readers which don't take "no" for an answer by CodesInChaos on Monday December 23, 2024 @11:02AM (#42495349)
Returning a 3xx redirect to an generic error page is even worse than 200.