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.

  • noride@lemm.ee
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    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.

  • Dunstabzugshaubitze@feddit.org
    link
    fedilink
    arrow-up
    0
    ·
    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
      link
      fedilink
      arrow-up
      0
      ·
      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.

  • vasametropolis@lemm.ee
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    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.

    • sus@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 months ago

      to be even more pedantic, if we follow the relevant official RFCs for http (formerly 2616, but now 7230-7235 which have relevant changes), a 403 can substitute for a 401, but a 401 has specific requirements:

      The server generating a 401 response MUST send a WWW-Authenticate header field (Section 4.1) containing at least one challenge applicable to the target resource.

      (the old 2616 said 403 must not respond with a request for authentication but the new versions don’t seem to mention that)

  • Feathercrown@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    2 months ago

    The last one is very convenient. As an API consumer you can get all the necessary info from the returned payload, and the 403 will trigger the error path.

  • kevincox@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    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.

  • Daemon Silverstein@thelemmy.club
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    A mix between the last one and the previous. The key error should be set in order to indicate the presence of an error, while status and code represents, respectively, the error numerical code and the error type, while the error message is supposed to be concatenated to an human-friendly error text.

  • snowe@programming.devM
    link
    fedilink
    arrow-up
    0
    ·
    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.

  • marcos@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    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
      link
      fedilink
      arrow-up
      0
      ·
      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.

  • SorteKanin@feddit.dk
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    A simple error code is sufficient in all of these cases. The error provided gives no additional information. There is no need for a body for these responses.

    • Kogasa@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

      There may be a need for additional information, there just isn’t any in these responses. Using a basic JSON schema like the Problem Details RFC provides a standard way to add that information if necessary. Error codes are also often too general to have an application specific meaning. For example, is a “400 bad request” response caused by a malformed payload, a syntactically valid but semantically invalid payload, or what? Hence you put some data in the response body.

      • SorteKanin@feddit.dk
        link
        fedilink
        arrow-up
        0
        ·
        2 months ago

        A plain 400 without explanation is definitely not great UX. But for something like 403, not specifying the error may be intentional for security reasons.

        • b_n@sh.itjust.works
          link
          fedilink
          arrow-up
          0
          ·
          2 months ago

          I know of some people that never use 403, but instead opt for 404 for security reasons. 403 implies that there is something they could have access to, but don’t.

          I think in some situations that this can be valid, but it shouldn’t be a crux.

          • SorteKanin@feddit.dk
            link
            fedilink
            arrow-up
            0
            ·
            2 months ago

            404 is definitely also used sometimes for hiding stuff that shouldn’t be seen, but 403 may still be appropriate for various stuff where there is nothing to hide. With 404 you probably also never want to give any explanation or error message.

  • CaptPretentious@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 months ago

    Of those options I’m going with the last one. Because you have the standard error number right there but I’m assuming that it’s a customer response so the message could be modified to have something useful in it.

  • killabeezio@lemm.ee
    link
    fedilink
    arrow-up
    0
    ·
    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.