How do we create ErrorHandler in retrofit 2.0? #1102

Closed
cooperkong opened this Issue Sep 21, 2015 · 18 comments

Projects

None yet
@cooperkong

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

Cheers,

@IgorGanapolsky

Also, how to detect network errors like:

RetrofitError.Kind.NETWORK

@JakeWharton
Member

IOExceptions are network errors

On Fri, Sep 25, 2015 at 12:56 PM Igor Ganapolsky notifications@github.com
wrote:

Also, how to detect network errors like:

RetrofitError.Kind.NETWORK

โ€”
Reply to this email directly or view it on GitHub
#1102 (comment).

@IgorGanapolsky

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

@ivacf
ivacf commented Oct 1, 2015

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?

@koesie10
koesie10 commented Oct 3, 2015

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

@JakeWharton
Member

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

Use an OkHttp interceptor.

@JakeWharton
Member

@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.

@rahulgautam

@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 ๐Ÿ‘
R.

@JakeWharton
Member

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.

@JakeWharton
Member

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.

@IgorGanapolsky

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" notifications@github.com 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
#1102 (comment).

@JakeWharton JakeWharton closed this in #1277 Nov 12, 2015
@MrThiago

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

@tomkoptel

Hi @MrThiago. Take a look on this gist.

@ViliusKraujutis

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

@rakshitsoni02
rakshitsoni02 commented Jul 26, 2016 edited

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 ?

@jemshit
jemshit commented Aug 19, 2016

Instead of RetrofitError.Kind.NETWORK, use IOException.

@tomkoptel

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

@jemshit
jemshit commented Aug 21, 2016

@tomkoptel i was just implementing that :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment