Created
April 5, 2017 12:56
-
-
Save joen93/7d4ae30ce29bef6127d74a26de151985 to your computer and use it in GitHub Desktop.
RxJava2 and Retrofit 2.2.0 compatible factory, which wraps the {@link RxJava2CallAdapterFactory} and takes care of the error conversion.
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
/** | |
* RxJava2 and Retrofit 2.2.0 compatible factory, | |
* which wraps the {@link RxJava2CallAdapterFactory} and takes care of the error conversion. | |
* | |
* Based on: https://github.com/square/retrofit/issues/1102#issuecomment-241250796 | |
*/ | |
public class RxErrorHandlingCallAdapterFactory extends CallAdapter.Factory { | |
private final RxJava2CallAdapterFactory mOriginalCallAdapterFactory; | |
private RxErrorHandlingCallAdapterFactory() { | |
mOriginalCallAdapterFactory = RxJava2CallAdapterFactory.create(); | |
} | |
public static CallAdapter.Factory create() { | |
return new RxErrorHandlingCallAdapterFactory(); | |
} | |
@Override | |
public CallAdapter<?, ?> get(final Type returnType, final Annotation[] annotations, final Retrofit retrofit) { | |
return new RxCallAdapterWrapper<>(retrofit, mOriginalCallAdapterFactory.get(returnType, annotations, retrofit)); | |
} | |
private static class RxCallAdapterWrapper<R> implements CallAdapter<R, Observable<R>> { | |
private final Retrofit mRetrofit; | |
private final CallAdapter<R, ?> mWrappedCallAdapter; | |
public RxCallAdapterWrapper(final Retrofit retrofit, final CallAdapter<R, ?> wrapped) { | |
mRetrofit = retrofit; | |
mWrappedCallAdapter = wrapped; | |
} | |
@Override | |
public Type responseType() { | |
return mWrappedCallAdapter.responseType(); | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public Observable<R> adapt(final Call<R> call) { | |
return ((Observable) mWrappedCallAdapter.adapt(call)).onErrorResumeNext(new Function<Throwable, ObservableSource>() { | |
@Override | |
public Observable apply(final Throwable throwable) { | |
return Observable.error(asRetrofitException(throwable)); | |
} | |
}); | |
} | |
private RetrofitException asRetrofitException(final Throwable throwable) { | |
// We had non-200 http error | |
if (throwable instanceof HttpException) { | |
final HttpException httpException = (HttpException) throwable; | |
final Response response = httpException.response(); | |
return RetrofitException.httpError(response.raw().request().url().toString(), response, mRetrofit); | |
} | |
// A network error happened | |
if (throwable instanceof IOException) { | |
return RetrofitException.networkError((IOException) throwable); | |
} | |
// We don't know what happened. We need to simply convert to an unknown error | |
return RetrofitException.unexpectedError(throwable); | |
} | |
} | |
} |
What is RetrofitException? Could you give an example if its something I should implement?
@challme28 Look at this: http://bytes.babbel.com/en/articles/2016-03-16-retrofit2-rxjava-error-handling.html
And here is the original RetrofitException: https://gist.github.com/koesie10/bc6c62520401cc7c858f#file-retrofitexception-java
Did anyone had this Exception?
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.reflect.Type retrofit2.CallAdapter.responseType()' on a null object reference
line 34.
Any ideas why mWrappedCallAdapter might be null?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is not working if I am using Completable getData(...) instead of ObservablegetData(...). Hope you could help us.