Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Handle errors gracefully and return standard error codes

Pragya Keshap answered on February 21, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • ensuresuccessstatuscode exception
  • Process completed with exit code 1.
  • This code will gives us an error
  • exit status 2

  • Handle errors gracefully and return standard error codes

    0

    To eliminate confusion for API users when an error occurs, we should handle errors gracefully and return HTTP response codes that indicate what kind of error occurred. This gives maintainers of the API enough information to understand the problem that’s occurred. We don’t want errors to bring down our system, so we can leave them unhandled, which means that the API consumer has to handle them.

    Common error HTTP status codes include:

    400 Bad Request – This means that client-side input fails validation.

    401 Unauthorized – This means the user isn’t not authorized to access a resource. It usually returns when the user isn’t authenticated.

    403 Forbidden – This means the user is authenticated, but it’s not allowed to access a resource.

    404 Not Found – This indicates that a resource is not found.

    500 Internal server error – This is a generic server error. It probably shouldn’t be thrown explicitly.

    502 Bad Gateway – This indicates an invalid response from an upstream server.

    503 Service Unavailable – This indicates that something unexpected happened on server side (It can be anything like server overload, some parts of the system failed, etc.).

    We should be throwing errors that correspond to the problem that our app has encountered. For example, if we want to reject the data from the request payload, then we should return a 400 response as follows in an Express API: 

    https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/

    Popularity 1/10 Helpfulness 1/10 Language typescript
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on Feb 21 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.