Get Response from an API that returns error 500

3

Next, I have an API that returns error 500 every time it breaks the business rule and returns a status and message. We know it's not a good practice to return status 500 under these circumstances, but is there any way I can get the JSON that it returns me, even it bursting 500 status?

It does not fall within then nor cath

    
asked by anonymous 22.05.2017 / 15:20

1 answer

2

Quite simply, you'll never get it, understand why.

The reason is, errors with code from 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 this range between 500 and 599 , should be treated as API failure and you should treat it even without knowing the reason for the failure. That is, check the httpstatus before anything, if it is between 500 and 599 , treat it as an API failure and do not expect it to return the reason for the failure.

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

Opposing the above situation, if the API handles a data type that is not expected, it is very likely to return a% cos_de error and the reason for the error.

Reference : List of HTTP status codes

    
29.05.2017 / 03:48