Question

Does anyone know what is the best format to return an HTTP response error to the client when facing an exception? A format to handle the error in a dynamic way and the easiest way to catch the exception between server and client

i suggested the following, but i need to get the best format for the error response:

   {
    "FLAG": "ERROR",
    "TEXTEN": "SERVER ERROR – BAD REQUEST"
    }
Was it helpful?

Solution

One good solution would be to send back the error code in the answer's headers, using the standardized HTTP codes from the HTTP protocol.

You can also add you own error code and a corresponding human readable message in the JSON body to provide more informations on the encountered error.

For example, for a Bad Request error, the corresponding HTTP code is 400. So you should send back the HTTP code 400 in the response header and the corresponding HTTP message (Bad Request). But some programming languages does not offer out of the box features to read the message. To be sure that the user can read it properly, you can also put it in the JSON body if you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top