New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we create ErrorHandler in retrofit 2.0? #1102

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

Comments

@cooperkong

cooperkong commented Sep 21, 2015

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

This comment has been minimized.

Show comment
Hide comment
@IgorGanapolsky

IgorGanapolsky Sep 25, 2015

Also, how to detect network errors like:

RetrofitError.Kind.NETWORK

IgorGanapolsky commented Sep 25, 2015

Also, how to detect network errors like:

RetrofitError.Kind.NETWORK

@JakeWharton

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton Sep 25, 2015

Collaborator

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

Collaborator

JakeWharton commented Sep 25, 2015

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

This comment has been minimized.

Show comment
Hide comment
@IgorGanapolsky

IgorGanapolsky Sep 25, 2015

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

IgorGanapolsky commented Sep 25, 2015

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

@ivacf

This comment has been minimized.

Show comment
Hide comment
@ivacf

ivacf 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?

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

This comment has been minimized.

Show comment
Hide comment
@koesie10

koesie10 Oct 3, 2015

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

koesie10 commented Oct 3, 2015

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

@JakeWharton

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton Nov 1, 2015

Collaborator

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

Use an OkHttp interceptor.

Collaborator

JakeWharton commented Nov 1, 2015

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

Use an OkHttp interceptor.

@JakeWharton

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton Nov 1, 2015

Collaborator

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

Collaborator

JakeWharton commented Nov 1, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@rahulgautam

rahulgautam Nov 7, 2015

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

rahulgautam commented Nov 7, 2015

@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

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton Nov 7, 2015

Collaborator

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.

Collaborator

JakeWharton commented Nov 7, 2015

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

This comment has been minimized.

Show comment
Hide comment
@JakeWharton

JakeWharton Nov 7, 2015

Collaborator

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.

Collaborator

JakeWharton commented Nov 7, 2015

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

This comment has been minimized.

Show comment
Hide comment
@IgorGanapolsky

IgorGanapolsky Nov 7, 2015

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

IgorGanapolsky commented Nov 7, 2015

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

@MrThiago

This comment has been minimized.

Show comment
Hide comment
@MrThiago

MrThiago Nov 23, 2015

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

MrThiago commented Nov 23, 2015

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

This comment has been minimized.

Show comment
Hide comment
@tomkoptel

tomkoptel Nov 27, 2015

Hi @MrThiago. Take a look on this gist.

tomkoptel commented Nov 27, 2015

Hi @MrThiago. Take a look on this gist.

@ViliusKraujutis

This comment has been minimized.

Show comment
Hide comment
@ViliusKraujutis

ViliusKraujutis Jul 5, 2016

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

ViliusKraujutis commented Jul 5, 2016

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

@rakshitsoni02

This comment has been minimized.

Show comment
Hide comment
@rakshitsoni02

rakshitsoni02 Jul 26, 2016

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 ?

rakshitsoni02 commented Jul 26, 2016

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

This comment has been minimized.

Show comment
Hide comment
@jemshit

jemshit Aug 19, 2016

Instead of RetrofitError.Kind.NETWORK, use IOException.

jemshit commented Aug 19, 2016

Instead of RetrofitError.Kind.NETWORK, use IOException.

@tomkoptel

This comment has been minimized.

Show comment
Hide comment
@tomkoptel

tomkoptel Aug 21, 2016

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 commented Aug 21, 2016

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

This comment has been minimized.

Show comment
Hide comment
@jemshit

jemshit Aug 21, 2016

@tomkoptel i was just implementing that :D

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