Skip to content Skip to sidebar Skip to footer

The Best Way To Debug Retrofit Error Messages

I'm new to Retrofit. I have used Volley, and I kind of like Retrofit. I was just about to select Retrofit when I ran into this very non-descriptive error message when trying to do

Solution 1:

Create a custom ErrorHandler for Retrofit.

I found that catching the error didn't provide a whole lot of extra information but creating a custom ErrorHandler for Retrofit allowed me to dig deeper into the actual error, like so:

classMyErrorHandlerimplementsErrorHandler {
  @Overridepublic Throwable handleError(RetrofitError cause) {
    Responser= cause.getResponse();
    if (r != null && r.getStatus() == 401) {
      returnnewUnauthorizedException(cause);
    }
    return cause;
  }
}

RestAdapterrestAdapter=newRestAdapter.Builder()
    .setEndpoint("https://api.github.com")
    .setErrorHandler(newMyErrorHandler())
    .setLogLevel(RestAdapter.LogLevel.FULL)  // Do this for development too.
    .build();

From the Custom Synchronous Error Handling section on Retrofit Page.

Set the Log Level to FULL as well, as shown in above config code.

Solution 2:

You probably want to add a catch clause to TestDriver.main:

try {
  service.logon();
} catch (RetrofitError e) {
  System.out.println(e.getResponse().getStatus());
}

Solution 3:

Unfortunately RetrofitError (1.6.1) was tricky. 401's made getResponse() always return null which made it hard to tell if its a connection issue or authentication issue. At least for me, I had to look at the message to get the 401 error. Hopefully this helps someone else trying to do something similar.

publicclassRetrofitErrorHandlerimplementsErrorHandler {

    @OverridepublicThrowablehandleError(RetrofitError cause) {

        if (cause.isNetworkError()) {
            if(cause.getMessage().contains("authentication")){
                //401 errorsreturnnewException("Invalid credentials. Please verify login info.");
            }elseif (cause.getCause() instanceofSocketTimeoutException) {
                //Socket TimeoutreturnnewSocketTimeoutException("Connection Timeout. " +
                        "Please verify your internet connection.");
            } else {
                //No ConnectionreturnnewConnectException("No Connection. " +
                        "Please verify your internet connection.");
            }
        } else {

            return cause;
        }
    }

}

Post a Comment for "The Best Way To Debug Retrofit Error Messages"