5

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?

Community
  • 1
  • 1
Suman
  • 1,307
  • 15
  • 32

2 Answers2

8

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
    // ...
  }
Community
  • 1
  • 1
Amir
  • 16,067
  • 10
  • 80
  • 119
  • thanks for reply @Amir, I see you link. But when net connection issues are arrive then enter into onFailure and return UnKnownHostException. i want exact particular reason of issues to the user through message?It can be solve by this? – Suman Sep 04 '16 at 06:42
  • It's depends what kinds of error you want to handle. for example in above code you can detect which type of network disconnect (Wifi | mobile) or in HttpException you can parse 500,400,401,404,.... – Amir Sep 04 '16 at 07:30
  • Hi Amir, i want to give you test case ,then you can understand. when my network disable and hit the api that enter failure block and give UnKnownHostException that not HttpException .So, in that time it not return any code .So how to handle it? – Suman Sep 05 '16 at 06:32
  • @Suman server doesn't return any status code? retrofit response.code() usually return proper code take a look at that may fix your issue. – Amir Sep 05 '16 at 07:00
  • Thanks @Amir for your Support, i can solve it .UnknownHoStException not HttpError .Need to Handle Like if (throwable instanceof UnknownHostException) {}. – Suman Sep 05 '16 at 07:35
1

Connection status should be checked anyway and has nothing to do with RetroFit, look up ConnectivityManager.

(a quicky solution is something like this, modify to your needs):

public boolean isConnected()
{
  ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo info = manager.getActiveNetworkInfo();

  return (info != null && info.isConnected());
}

EDIT: Consider this - ConnectivityManager let's you know you have an interface for outgoing data, if you want to know if you actually have an outside link, I would suggest to ping you server (or any other known domian that would respond), if the ping is good, you have a line, for Timeouts and serverNotFound - use the http codes you get back in onFailure() for an api request (e.g. 404, 400, 200, etc.).

The Why

A ping is just a single udp packet that let's you know the server is there and alive, so a scenario where a ping works, but an http request will return Bad Request is very possible, and can be handled easily, the logic is up to you.

For the other stuff - all the info you need to implement the handling you want can be found in the callbacks.

this example uses ResponseBody, you can get the delivery info from the header:

@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
{
  try
  {
   String response = rawResponse.headers().toString();
   //now print this...
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }
}

Timeouts and serverNotFound can be handeled through the onFailure callback (this is the skeleton, implement what you need inside it, read up about it):

@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable)
{
 //use throwable.get.... to know what happened
 throwable.printStackTrace();
}

Hope this helps in any way

TommySM
  • 3,793
  • 3
  • 24
  • 36
  • thanks for reply, ConnectivityManager only check wifi enable or not and data enable or not , After checking this and hit api the but data is not available in that case its goes to OnFailure block .In that case i want to handle connectin error and server error..there are any way to handle this? i want to show all error and issues user through message – Suman Sep 04 '16 at 06:23
  • edited the answer, take a look, keep in mind that you have to catch and understand the response yourself. – TommySM Sep 04 '16 at 07:51