Michael
Michael

Reputation: 89

Retrofit 2.0 & RxJava - Custom Error Handler

I'm using Retrofit 2.0 along with RxJava and I need to make a custom error handler so that I can get the HTTP error code. This is the example that I get, but it won't work on Retrofit 2.0, since RetrofitError is removed.

@Override
public void onError(Throwable e) {
 if (e instanceof RetrofitError) {
    if (((RetrofitError) e).isNetworkError()) {
        //handle network error
    } else {
        //handle error message from server
    }
 }
}

Is there any way that I can do this? Thanks.

Upvotes: 0

Views: 3269

Answers (1)

Michael
Michael

Reputation: 89

I'm sorry that I didn't read the documentation carefully, so I just find a simple answer that works

@Override
 public void onError(Throwable e) {
    HttpException exception = (HttpException) e;
    Toast.makeText(getApplicationContext(), exception.code() + "", Toast.LENGTH_SHORT).show();
 }

Upvotes: 1

Related Questions