Last active
June 21, 2022 08:55
-
-
Save julianfalcionelli/a585d2cb2fbaca2e123ae7df3e36fa39 to your computer and use it in GitHub Desktop.
Rx Error Handling for Retrofit2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Thank you for this! Saved me a lot of work.
Does RxJava trigger onErrorResumeNext on Http 404 response codes?
Is this compatible with rxjava2 & retrofit2 ?
Yes it is
Thanks you
Thanks for the code but the can we get ServerException example
Finally found what I needed. Thank you so much!
I had a lot of problems with this.
thanks for your hepls
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
Just thank you!