Retrofit: How do we create ErrorHandler in retrofit 2.0?

Created on 21 Sep 2015  Â·  18Comments  Â·  Source: square/retrofit

Hi
It seems ErrorHandler is removed in 2.0 and we need to use CallAdapter instead. Is there any examples to look at?

Cheers,

Needs Info

Most helpful comment

If you like the terrible API that was RetrofitError (and in that gist is RetrofitException) then go for it. I find that API to awful (which I'm allowed to say as the author) and it forces creating messy and bug-prone code in the caller.

All 18 comments

Also, how to detect network errors like:

RetrofitError.Kind.NETWORK

IOExceptions are network errors

On Fri, Sep 25, 2015 at 12:56 PM Igor Ganapolsky [email protected]
wrote:

Also, how to detect network errors like:

RetrofitError.Kind.NETWORK

—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1102#issuecomment-143280769.

It would be nice if you actually demonstrated handling this type of error in the sample, or in your u2020 app...

We were using ErrorHandler to intercept HTTP 401 errors for any request to the API. Is there a way to achieve this with Retrofit 2 without having to check every single error separately?

I've modified ExecutorCallAdapterFactory to handle errors like in Retrofit 1:
https://gist.github.com/koesie10/bc6c62520401cc7c858f

We were using ErrorHandler to intercept HTTP 401 errors for any request to the API.

Use an OkHttp interceptor.

@cooperkong Please describe what you are trying to do. ErrorHandler does not exist any more because the terribly-designed RetrofitError class has also been eliminated.

@JakeWharton Should we handle error response model mapping this _gist_ way?
Do you consider this gist a good example for that?

One more thing, Is it early to use Retrofit 2 in a production project?

Thank you for making android development awesome :+1:
R.

If you like the terrible API that was RetrofitError (and in that gist is RetrofitException) then go for it. I find that API to awful (which I'm allowed to say as the author) and it forces creating messy and bug-prone code in the caller.

Is it early to use Retrofit 2 in a production project?

No. It's functionally robust and really just in the stage of solidifying any APIs, behaviors, and features before release.

The only problem I ran into with Retrofit 2 is that it forces usage of
Okhttp as the client. So I couldn't get around an HTTPS connection error
from an older server, because apparently Okhttp doesn't work with such old
TLS and Cipher versions.
On Nov 7, 2015 7:20 AM, "Jake Wharton" [email protected] wrote:

Is it early to use Retrofit 2 in a production project?

No. It's functionally robust and really just in the stage of solidifying
any APIs, behaviors, and features before release.

—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1102#issuecomment-154695820.

I need to handle Errors in my App, If user fail login, the server will return code 400 with a error message. At moment I can not get the body to read the message.
I came I cross this article http://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/ under Errors handling. Does anyone knows how to implement this into retrofit 2.0 ?
thanks

Hi @MrThiago. Take a look on this gist.

@tomkoptel, that gist is not working with latest Retrofit2 version.

Hello @JakeWharton ,

I was throwing custom exception in Error handler for example :

`@Singleton
public class Msg91CommunicationErrorHandler implements ErrorHandler {
  private static final Logger LOGGER = LoggerFactory.getLogger(Msg91CommunicationErrorHandler.class);

  @Inject
  public Msg91CommunicationErrorHandler() {
  }

  @Override
  public Throwable handleError(RetrofitError cause) {
    LOGGER.error("Error received while communicating with msg91", cause);
    return new Msg91CommunicationException("response= " + createExceptionMessage(cause));
  }

  protected String createExceptionMessage(RetrofitError cause) {
    return cause.getResponse() + cause.getMessage();
  }

}`

How can i achieve the same through retrofit 2 ?

Instead of RetrofitError.Kind.NETWORK, use IOException.

Yet, another approach to force same error handling pattern from Retrofit 1 into Retrofit 2 can be find here Retrofit 2 and Rx Java call adapter error handling

@tomkoptel i was just implementing that :D

Was this page helpful?
0 / 5 - 0 ratings