Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle REST Exceptions?

We are in the middle of a ongoing discussion about how to handle REST exceptions.

Response Content type : JSON

Two solutions we have:

  1. Throw all the unchecked exceptions as a JSON response.
  2. Send Request Invalid Response code.

Arguments:

  • When its a error, why return JSON? Just send a invalid response code.

Counter Argument:

  • Response code are too technical to handle for normal developers.

Whats your say??

like image 567
Vanchinathan Chandrasekaran Avatar asked Jun 25 '10 21:06

Vanchinathan Chandrasekaran


People also ask

What are the ways to handle exceptions?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

How do you handle exceptions in Microservices?

First, we need to set up the capability of throwing exceptions on core banking service errors. Open core banking service and follow the steps. Create a common exception class where we going to extend RuntimeException. After that, we can create custom runtime exceptions to use with this API.

How do I throw an exception in REST API?

Go to the flow of the REST API method or the callback (such as OnAuthentication or OnRequest) where you want to throw the error and add a Raise Error element. Set the Exception property to the User Exception you have created. Set the Exception Message property to your custom error message.


2 Answers

For a JSON API I recently developed I do both. I always respond with valid JSON (well, assuming I respond at all). If I detect an invalid request, I use status 400. If I detect a server error (which I don't believe is caused by an invalid request), I use a 5xx status. The JSON object contains a special key that is only set for errors, with a string value.

I think this is a good solution that respects REST principles, and can be used in multiple ways. The same solution is used by some other JSON APIs, such as Yahoo Search. Try http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&output=json .

like image 137
Matthew Flaschen Avatar answered Oct 21 '22 04:10

Matthew Flaschen


Use error codes like for HTTP. So 50* for any exception cause by some internal problem. And 40* for bad arguments. Avoid using your own defined codes as far as its possible. The idea is to have a "uniform" interface.

In general. 204 for success without sending any content 200 for success with a json representation of the resource And if its not a successful operation return appropriate response code. You can choose to optionally return a json. To simplify things you can have a common format (json) for all error responses.

http://en.wikipedia.org/wiki/REST is a must read before you freeze on your api specs.

like image 35
neal aise Avatar answered Oct 21 '22 06:10

neal aise