Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save julianfalcionelli/a585d2cb2fbaca2e123ae7df3e36fa39 to your computer and use it in GitHub Desktop.
Save julianfalcionelli/a585d2cb2fbaca2e123ae7df3e36fa39 to your computer and use it in GitHub Desktop.
Rx Error Handling for Retrofit2
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import retrofit2.Call;
import retrofit2.CallAdapter;
import retrofit2.HttpException;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
/**
* original in http://bytes.babbel.com/en/articles/2016-03-16-retrofit2-rxjava-error-handling.html
*/
public class RxErrorHandlingCallAdapterFactory extends CallAdapter.Factory
{
private final RxJava2CallAdapterFactory original;
private RxErrorHandlingCallAdapterFactory()
{
original = RxJava2CallAdapterFactory.create();
}
public static CallAdapter.Factory create()
{
return new RxErrorHandlingCallAdapterFactory();
}
@SuppressWarnings("NullableProblems")
@Override
public CallAdapter get(Type returnType, Annotation[] annotations, Retrofit retrofit)
{
return new RxCallAdapterWrapper(retrofit, original.get(returnType, annotations, retrofit));
}
private static class RxCallAdapterWrapper implements CallAdapter
{
private final Retrofit retrofit;
private final CallAdapter wrapped;
public RxCallAdapterWrapper(Retrofit retrofit, CallAdapter wrapped)
{
this.retrofit = retrofit;
this.wrapped = wrapped;
}
@Override
public Type responseType()
{
return wrapped.responseType();
}
@SuppressWarnings({"unchecked", "NullableProblems"})
@Override
public Object adapt(Call call)
{
Object adaptedCall = wrapped.adapt(call);
if (adaptedCall instanceof Completable) {
return ((Completable) adaptedCall).onErrorResumeNext(
throwable -> Completable.error(asRetrofitException(throwable)));
}
if (adaptedCall instanceof Single) {
return ((Single) adaptedCall).onErrorResumeNext(
throwable -> Single.error(asRetrofitException((Throwable) throwable)));
}
if (adaptedCall instanceof Observable) {
return ((Observable) adaptedCall).onErrorResumeNext(
(Function<? super Throwable, ? extends ObservableSource>)
throwable -> Observable.error(asRetrofitException(throwable)));
}
throw new RuntimeException("Observable Type not supported");
}
private ServerException asRetrofitException(Throwable throwable)
{
// We had non-200 http error
if (throwable instanceof HttpException)
{
HttpException httpException = (HttpException) throwable;
Response response = httpException.response();
return ServerException.httpError(response.raw().request().url().toString(),
response, retrofit);
}
// A network error happened
if (throwable instanceof IOException)
{
return ServerException.networkError((IOException) throwable);
}
// We don't know what happened. We need to simply convert to an unknown error
return ServerException.unexpectedError(throwable);
}
}
}
@alextcn
Copy link

alextcn commented Aug 16, 2017

Just thank you!

@j-garin
Copy link

j-garin commented Sep 8, 2017

Thank you for this! Saved me a lot of work.

@mddtrifecta
Copy link

Does RxJava trigger onErrorResumeNext on Http 404 response codes?

@rhonyabdullah
Copy link

Is this compatible with rxjava2 & retrofit2 ?

@AhmedKamal1992
Copy link

Yes it is

@ThanhLD24
Copy link

Thanks you

@Anmolk22
Copy link

Thanks for the code but the can we get ServerException example

@m-toshpulatov
Copy link

Finally found what I needed. Thank you so much!

@jnasirur
Copy link

I had a lot of problems with this.
thanks for your hepls

@ajocardBuchi
Copy link

Please can you help send a Kotlin implementation

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