Get Response from an API that returns error 500

Asked

Viewed 2,569 times

3

Next, I have an API that returns error 500 every time it violates the business rule by returning me a status and a message. We know it’s not good practice to return status 500 under these circumstances, but there’s some way I can get the JSON back to me, even though it blows status 500?

He won’t even fall for then nor in the cath

  • 1

    Put the code in for the guys to help you better

1 answer

2

Very simply, you’ll never make it, understand why.

The reason is, errors with code 500 to 599, are usually associated with errors in the server application, which failed to operate a request.

What you should do is first, any error within that interval between 500 and 599, should be treated as a failure in the API and you should treat it even without knowing the reason for the failure. That is, check the httpstatus before anything, if you are among 500 and 599, treat as an API failure and don’t expect it to return the reason for the failure.

Keep in mind that if you send a value the API does not expect and it returns an error 500 for example, it is probably because the type of value you are sending is unexpected.

Contrary to the above situation, if the API treats a type of data that is not expected, it is very likely to return an error to you 400 and the reason for the error.

Reference: List of HTTP status codes

Browser other questions tagged

You are not signed in. Login or sign up in order to post.