131
submitted 2 months ago* (last edited 2 months ago) by iso@lemy.lol to c/programming@programming.dev

I prefer simplicity and using the first example but I'd be happy to hear other options. Here's a few examples:

HTTP/1.1 403 POST /endpoint
{ "message": "Unauthorized access" }
HTTP/1.1 403 POST /endpoint
Unauthorized access (no json)
HTTP/1.1 403 POST /endpoint
{ "error": "Unauthorized access" }
HTTP/1.1 403 POST /endpoint
{
  "code": "UNAUTHORIZED",
  "message": "Unauthorized access",
}
HTTP/1.1 200 (🤡) POST /endpoint
{
  "error": true,
  "message": "Unauthorized access",
}
HTTP/1.1 403 POST /endpoint
{
  "status": 403,
  "code": "UNAUTHORIZED",
  "message": "Unauthorized access",
}

Or your own example.

top 50 comments
sorted by: hot top controversial new old
[-] gencha@lemm.ee 96 points 2 months ago

Respect the Accept header from the client. If they need JSON, send JSON, otherwise don't.

Repeating an HTTP status code in the body is redundant and error prone. Never do it.

Error codes are great. Ensure to prefix yours and keep them unique.

Error messages can be helpful, but often lead developers to just display them in the frontend, breaking i18n. Some people supply error messages in multiple languages, depending on the Accept-Language header.

[-] Michal@programming.dev 32 points 2 months ago

This guy backends ☝️

[-] pe1uca@lemmy.pe1uca.dev 7 points 2 months ago

but often lead developers to just display them in the frontend

Oh boy I feel this one.
My API is meant for scripting (i.e. it's for developers and the errors are for developers), but the UI team uses it and they just straight display the error from their HTTP request for none technical people which might also not get to know all the parameters actually needed for the request.
And even when the error is in fact in my code, and I sent all the data I need to debug and replicate the error, the users can't tell me because the UI truncates the response, so the user only sees something like Error in pe1uca's API: {"error":"bad request","message":"Your request has an error, please check th... (truncated). So the message gets truncated and the link to the documentation is also never shown .-.

[-] FizzyOrange@programming.dev 5 points 2 months ago

To be fair if it's an exceptional error message (i.e. database timeout; not incorrect password) I don't think i18n matters that much. Most people will just be googling the error message anyway, and if not it should be rare enough that using Google translate isn't an issue.

[-] azertyfun@sh.itjust.works 8 points 2 months ago

If anything i18n makes things way worse for everyone. Ever tried to diagnose a semi-obscure Windows or Android error on a non-English locale? Pretty sure that's one of the activities in the inner circles of Hell. Bonus points if the error message is obviously machine-translated and therefore semantically meaningless.

Unique error codes fix this if they remain visible to the user, which they usually don't because Mr Project Manager thinks it looks untidy.

load more comments (1 replies)
[-] ramble81@lemm.ee 67 points 2 months ago

Giving back a 200 for an error always makes me bristle. Return correct codes people. “But the request to the web server was successful!”

[-] FizzyOrange@programming.dev 16 points 2 months ago

I use this big expensive simulator called Questa, and if there's an error during the simulation it prints Errors: 1, Warnings: 0 and then exits with EXIT_SUCCESS (0)! I tried to convince them that this is wrong but they're like "but it successfully simulated the error". 🤦🏻‍♂️

We end up parsing the output which is very dumb but also seems to be industry standard in the silicon industry unfortunately (hardware people are not very good at software engineering).

[-] Dark_Arc@social.packetloss.gg 11 points 2 months ago* (last edited 2 months ago)

That's when you use different exit codes. 1 for failure during simulation, 2 for simulation failed.

Shame they wouldn't listen.

load more comments (1 replies)
[-] OneCardboardBox@lemmy.sdf.org 4 points 2 months ago

I worked on a product that was only allowed to return 200 OK, no matter what.

Apparently some early and wealthy customer was too lazy to check error codes in the response, so we had to return 200 or else their site broke. Then we'd get emails from other customers complaining that our response codes were wrong.

load more comments (3 replies)
[-] xiodine@programming.dev 27 points 2 months ago
[-] iso@lemy.lol 4 points 2 months ago

This one looks nice. Very detailed.

[-] Metju@lemmy.world 4 points 2 months ago

This is the right answer imo. While it might be an overkill for sth like 404s, it's amazing for describing different bad requests.

[-] madeindjs@programming.dev 3 points 2 months ago

I don't get why the RFC show an example returning 403 with body "You do not have enough credit." although there is a dedicated status code " 402 Payment Required". Isn't more correct to use 402 in this situation?

[-] pe1uca@lemmy.pe1uca.dev 2 points 2 months ago

Don't know what are the changes since 7807 (which this one obsoletes) but this article helped me quickly understand the first one, hopefully it's still somewhat relevant.
https://lakitna.medium.com/understanding-problem-json-adf68e5cf1f8

[-] houseofleft@slrpnk.net 23 points 2 months ago* (last edited 2 months ago)

I'm a data engineer, and have seen an ungodly ammount of 200-but-actually-no-stuff-is-broken errors and it's the bane of my life!

We have generic code to handle pulling in api data, and transforming it. It's obviously check the status code, but any time an API implements this we have to choose between:

  • having code fail wierdly further down the line because can't parse the status
  • adding in some kind of insane if not response.ok or "actually no there's an error really" in response.content logic

Every time you ignore protocols and invent your own, you are making everyone sad.

Will take recommendations of support groups I can join for victims of terrible apis.

[-] magic_lobster_party@fedia.io 22 points 2 months ago

I think the general rule of thumb is: Keep it Simple, Stupid.

Don’t include fields “just in case”. If you don’t have a use for a field right now, then don’t include it. It’s often easier to add fields than removing.

Avoid having fields that can be derived from other fields. Code “UNAUTHORIZED” can be derived from 403. Having both adds confusion. It adds the question whether the code field be something other than “UNAUTHORIZED” when the response is 403.

Just 403 with empty body is fine. Add message in a JSON in case it’s useful for the user. If the user needs more fields in the future, then it’s easy to expand the JSON.

[-] huginn@feddit.it 15 points 2 months ago

403 is a category, not a code. Yes I know they're called http codes but REST calls are more complex than they were in 2001. There are hundreds of reasons you might not be authorized.

Is it insufficient permissions? Authentication required? Blocked by security? Too many users concurrently active?

I'd argue the minimum for modern services is:

403 category
Code for front end error displays
Message as default front end code interpretation

As json usually but if you're all using protobuf, go off King.

load more comments (8 replies)
[-] elrik@lemmy.world 18 points 2 months ago

JSON Problem Details

https://datatracker.ietf.org/doc/html/rfc9457

  • It has a specification, so a consumer of the API can immediately know what to expect.
  • It has a content type, so a client sdk can intelligently handle the response.
  • It supports commonly needed members which are a superset of all of the above JSON examples, including type for code and repeating the http status code in the body if desired.
  • It is extensible if needed.
  • It has been defined since at least 2016.

This specification's aim is to define common error formats for applications that need one so that they aren't required to define their own ...

So why aren't you using problem details?

[-] LaggyKar@programming.dev 17 points 2 months ago

It's 401 unauthorized or 403 forbidden, not 403 unauthorized

[-] iso@lemy.lol 6 points 2 months ago

You’re right, I was just giving an example though.

load more comments (1 replies)
[-] ShortFuse@lemmy.world 14 points 2 months ago* (last edited 2 months ago)

Don't use JSON for the response unless you include the response header to specify it's application/json. You're better off with regular plaintext unless the request header Accept asked for JSON and you respond with the right header.

That also means you can send a response based on what the request asked for.

403 Forbidden (not Unauthorized) is usually enough most of the time. Most of those errors are not meant for consumption by an application because it's rare for 4xx codes to have a contract. They tend to go to a log and output for human readers later, so I'd lean on text as default.

[-] BrianTheeBiscuiteer@lemmy.world 2 points 2 months ago

I would actually encourage error responses be in JSON if your 200 responses are JSON. Some clients are apt to always convert the body to JSON so it could avoid an exception on the client side not to throw a curveball.

To your point it's most important that the content and Content-Type header match.

[-] lemmyvore@feddit.nl 4 points 2 months ago

If any client app is blindly converting body to JSON without checking (at the very least) content type and size, they deserve what they get.

If you want to make it part of your API spec to always return JSON that's one thing, but don't do it to make up for poorly written clients. There's no end of ways in which clients can fail. Sticking to a clear spec is the only way to preserve your sanity.

[-] fart_pickle@lemmy.world 13 points 2 months ago

I don't have a response to share but I always lose my mind when I see AWS error messages, especially when using bazillion layers like CDK for Terraform, executed from the shell script that runs a python script in the CI/CD pipeline.

One of the issues I will never forget was the debugging of permission issue. Dev reported an issue, something like "cannot access the SQS queue from a recently deployed script". The error message was like "cannot access the queue due to missing policy in assumed role" (or something similar). So, I have checked the python script and related policies - all good. Next I've moved to a shell script, still no luck. After that I went through the CDK files, no issues. I was about to involve the AWS support when it turned out that the queue name has been changed manually in the AWS console. AWS, instead of point out that the queue is missing, raised an error about missing access permissions...

load more comments (2 replies)
[-] calcopiritus@lemmy.world 13 points 2 months ago

My favourite is when every error is an HTTP Bad Request with no body. Absolutely wonderful to use those APIs

[-] Dunstabzugshaubitze@feddit.org 12 points 2 months ago

since none of your examples add anything of value in the body: a plain old 403 is enough.

response bodies for 400 responses are more interesting, since you can often tell why a request was bad and the client can use that information to communicate to the user what went wrong.

best error code remains 418, though.

[-] epyon22@programming.dev 3 points 2 months ago

I was annoyed that the one time I wanted to use 418 as a filler Dotnets http library didn't support returning it.

load more comments (1 replies)
[-] snowe@programming.dev 12 points 2 months ago

Anything but the last one. Don't duplicate the http code in the body, else you're now maintaining something you don't need to maintain.

I'm not a fan of codes that repeat information in the body either, but I think if you had used a different example like "INVALID_BLAH" or something then the message covered what was invalid, then it would be fine. Like someone else said, the error data should be in an object as well, so that you don't have to use polymorphism to figure out whether it's an error or not. That also allows partially complete responses, e.g. data returns, along with an error.

[-] lengau@midwest.social 12 points 2 months ago

At a previous job we had an unholy combination of the last two:

HTTP/1.1 200 POST /endpoint 
{
  "data": null,
  "errors": ["403", "unauthorized"],
  "success": false
}
load more comments (2 replies)
[-] Tellore@lemmy.world 11 points 2 months ago

When consuming APIs you often want JSON in successful scenario. Which means, if you also have JSON in unsuccessful scenario it's a bit more uniform, because you don't have to deal with JSON in one case and plaintext response in other. Also, it sometimes can be useful to have additional details there like server's stacktrace or some identifiers that help troubleshoot complex issues.

[-] traches@sh.itjust.works 6 points 2 months ago

Probably not great to return server stack traces. Otherwise, yeah

load more comments (1 replies)
[-] Boomkop3@reddthat.com 8 points 2 months ago

just 403 and leave the body empty

[-] BrianTheeBiscuiteer@lemmy.world 2 points 2 months ago

Good enough in most cases. Too much info and it might as well give step by step instructions on how to hack you.

[-] GetOffMyLan@programming.dev 8 points 2 months ago* (last edited 2 months ago)

I like using Problem detials

It's fully supported by the .net server pretty much out the box and just seems nice to stick to a standard where possible.

[-] killabeezio@lemm.ee 7 points 2 months ago

The status code that gets returned should be the status code of the messenger and not the data. If you want to add a status code about the data, then please do.

If something can return null and empty and it's valid, that is not a 404. That is a 200.

As far as a 403, the messenger is telling you that you shall not pass. There is no data. 403 is appropriate here. The return response can be anything since 403 is pretty self explanatory, but I would probably return json to be consistent. I would also use the field message. Something like the first one for this use case only.

In other cases where i do get data, I would use data, message, status (optional). But status in the json response would be status about the message.

[-] vasametropolis@lemm.ee 7 points 2 months ago* (last edited 2 months ago)

1 or 4 but wrapped in a top level error object. It’s usually best to not use the top level namespace because then you can’t add meta details about the request easily later without changing the original response schema.

Codes are great but I’m usually too lazy to introduce them right away, so I instead have message (which is guaranteed to come back) and context, which is any JSON object and doesn’t adhere to a guaranteed structure. Another poster pointed out that code is way easier for localization since you are probably not localizing your message.

The HTTP status code is generally sufficient to describe what happened without having to catalogue every error with a unique “code”. A context blob is useful for dumping validation errors or any other details about the error that a human could at least rely on for help.

Putting the status code on the body seems helpful but is actually useless, since the only place you can assume it’s always provided is on the response itself and not the body.

[-] kevincox@lemmy.ml 6 points 2 months ago
HTTP/1.1 403 UNAUTHORIZED
{
  "error": {
    "status": "UNAUTHORIZED",
    "message": "Unauthorized access",
  },
}

I would separate the status from the HTTP status.

  1. The HTTP status is great for reasonable default behaviours from clients.
  2. The application status can be used for adding more specific errors. (Is the access token expired, is your account blocked, is your organization blocked)

Even if you don't need the status now, it is nice to have it if you want to add it in the future.

You can use a string or an integer as the status code, string is probably a bit more convenient for easy readability.

The message should be something that could be sent directly to the user, but mostly helpful to developers.

[-] DaniloT@lemmy.world 6 points 2 months ago

Message straight on the body is the worst possible response for an error here, it is bad design to straight up show the error from the back end to the user, usually it needs translation and/or adaptation due to message size on the front-end to show properly, and applying those on top of a message will make it stop working as soon as anyone in the backend decide to change a dot or comma anywhere. It is a bad idea to let the backend make direct impact in the front when you can because backend devs won't even know what impact they are causing until later in testing and it will be harder to trace back and fix.

IMO you need at least a json with code and message, the front will ignore the message for everything but testing and use the code to match a translation file that will get the proper message, making it easy to translate and change as needed without having to rebuild the whole backend along with front changes. You may also have an extra parameter there in some cases when you want to return where more specifically the error occurred or an array of errors. Status usually not needed as you can get those from the http code itself.

load more comments (2 replies)
[-] VonReposti@feddit.dk 6 points 2 months ago

The documented one. It is hell to work with APIs where only the happy path is documented.

[-] marcos@lemmy.world 6 points 2 months ago* (last edited 2 months ago)

Have a code, where you can really describe the error; try to use the correct HTTP status (your example doesn't); don't ever use status 200 for errors; and finally, have an "error" key set to something somewhere (I'd write the error code to it).

The message is optional.

So, the simplest version would be:

HTTP/1.1 401 POST /endpoint
{
     "error": "UNAUTHORIZED"
}
[-] kogasa@programming.dev 4 points 2 months ago

Status 200 for errors is common for non-REST HTTP APIs. An application error isn't an HTTP error, the request and response were both handled successfully.

[-] redline23@lemmy.world 5 points 2 months ago

I like the fourth or the last one since it encourages all other error responses to follow a similar standard. That will allow the client to have a reusable error model and error checking.

I've had to use APIs where every response was 200 ok with json, 400 bad request with pain text that said unauthorized, or a 500 error that returned an HTML error page. The worst.

[-] tun@lemm.ee 5 points 2 months ago* (last edited 2 months ago)

GitHub has OpenAPI specification. Latest version is 3.1, I think.

[-] iso@lemy.lol 3 points 2 months ago

Looks like they're recommending object of error code (number) and message.

[-] AsudoxDev@programming.dev 4 points 2 months ago* (last edited 2 months ago)

Is the last one real? Has any sane dev made something like that monstrosity? It's not like the others are any better, but who would ever think of doing the last one and why?

[-] FourPacketsOfPeanuts@lemmy.world 4 points 2 months ago* (last edited 2 months ago)

I have worked for financial institutions that have variations of the last one. If I saw it I wouldn't even blink. Semi realistic reasons might be:

Status attribute - because the project is using the base library of [project whatever] which was the brain child of eNtErPrIsE aRcHiTeCt whose hands on skills are useless and the off-shore dev team who assigned [random newbie] because that's who was available at the time. They used a status attribute because they didn't know how to get the status of the http response. No-one with budget control is interested in hearing about technical debt at the moment. Everyone has to use it now else the poorly written test classes fail.

Message code: because "we need codes that won't ever change even if the message does". Bonus points if this is, in fact, never used as intended and changes more frequently than...

Message: "because we still need to put something human readable in the log". Bonus points x2 if this is localised to the location of the server rather than the locale of the request. Bonus x3 if this is what subsequent business logic is built on leading to obscure errors when the service is moved from AWS East Virginia to AWS London (requests to London returning "colour" instead of "color" break [pick any service you never thought would get broken by this]).

I have seen it all etc

[-] hex@programming.dev 4 points 2 months ago* (last edited 2 months ago)

For me it just depends on what I expect. They're all relatively the same thing. As long as the status code is appropriate (403), it doesn't matter whether it's JSON or plaintext. Ideally the API would respect and handle the request header, and return plaintext if you request plaintext.

[-] tja@sh.itjust.works 3 points 2 months ago

I really like the fifth one. So you might always get a surprise message in your response

[-] noride@lemm.ee 2 points 2 months ago* (last edited 2 months ago)

I like the last one, I think having the status code in the body could help clarify where the error is coming from when traversing a reverse proxy.

load more comments
view more: next ›
this post was submitted on 30 Aug 2024
131 points (99.2% liked)

Programming

17314 readers
270 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 1 year ago
MODERATORS