Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle different types of exceptions and errors in retrofit 2.0.1

I am using Retrofit 2.0.1. I want to handle all types of network errors and exceptions (like no network connection ,timeout error,server not found etc.) I have found this link. But some of the methods were deprecated in v1.8.0. How can I do that in 2.0.1?

like image 410
Suman Avatar asked Sep 02 '16 13:09

Suman


People also ask

How do you handle errors in the destructor?

9. How to handle error in the destructor? Explanation: It will not throw an exception from the destructor but it will the process by using terminate() function. 10.

What is call in retrofit?

Think of Call as a simple class which wraps your API response and you need this class make an API call and provide listeners/callback to notify you with error and response , although if you use kotlin coroutines then after version 2.6.


1 Answers

If you need centeralize error handler take a look at this thread But if you just need a simple error handler you can do:

 @Override
 public void onFailure(Throwable throwable) {
    if (throwable instanceof HttpException) {
       // We had non-2XX http error
    }
    if (throwable instanceof IOException) {
       // A network or conversion error happened
    }

    // We don't know what happened. We need to simply convert to an unknown error
    // ...
  }
like image 127
Amir Avatar answered Sep 25 '22 18:09

Amir