19

I'm trying to figure out the right way to do error handling in Retrofit synchronous calls. I know for asynchronous calls, Retrofit has a callback for failure case. But how should I handle error for synchronous call? My guess is wrapping the call with a try block and handle RetrofitError exception in catch block.

Fei Qu
  • 999
  • 2
  • 12
  • 26
  • 1
    Why do you want to use synchronous call in Retrofit? Http requests should be sent asynchronously in Android. – Piotr Wittchen May 18 '15 at 19:03
  • 18
    @piotr.wittchen sometimes you're already in a thread, then it makes perfectly good sense to use a synchronous call – Kevin van Mierlo May 18 '15 at 19:08
  • Just adding to that, I think its also needed when you are trying to refresh a token in background after receiving 401 from the sever. – Shobhit Puri Jan 29 '16 at 21:47
  • If you're using Android Priority Job Queue (Job Manager) then you use synchronous calls in the job, it has its uses, I personally don't want to have network calls in the ui classes (Activities) – CaptRisky Sep 06 '16 at 14:06

2 Answers2

6

Your guess seems correct, using synchronous calls Retrofit is made to throw a RetrofitError representing the error: Reference. Note that the throw IllegalStateException in handleError shouldn't happen in the case of a synchronous call.

Edit: It appears Retrofit is slowly moving on to the 2.0 release, if you plan on using Retrofit 2.0, I recommend reading the documentations to see how it is done in the new version.

Edit pt2: Retrofit has moved to 2.0 release and now if you want to handle errors you no longer have to catch RetrofitErrors but IOException. You can directly have a look at the implementation of execute()

/**
 * Synchronously send the request and return its response.
 *
 * @throws IOException if a problem occurred talking to the server.
 * @throws RuntimeException (and subclasses) if an unexpected error occurs creating the request
 * or decoding the response.
 */
Response<T> execute() throws IOException;

Other references: 1

Community
  • 1
  • 1
Bhullnatik
  • 1,263
  • 17
  • 28
4

It's hard to find this. Nobody really talks about the error handling on synchronous calls. But I found something. I'm not entirely sure if the next line should be added (it should definitely be added for custom errors, but this is not the case) I found it here

Foo doFoo() throws RetroFitError;

The synchronous call should be happening inside a try catch clause like this:

try{
    doFoo();
}catch(RetroFitError e){

}

Found here

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • Hi Kevin, thanks for your answer. Based on the source https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/RestAdapter.java#L182 and https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/ErrorHandler.java there's already a default handler which throws RetrofitError. So if we don't need to handle custom error, we don't have to throws the RetrofitError specifically. – Fei Qu May 18 '15 at 20:52
  • @feiqu Ohh okay. Then you can leave that away. The try catch method will be your error handler then ;) – Kevin van Mierlo May 18 '15 at 21:27