我有下一个处理错误的代码。此代码非常适合处理错误,但我想处理“未授权”错误。
public class RxErrorHandlingCallAdapterFactory extends CallAdapter.Factory {
private RxJava2CallAdapterFactory rxJavaCallAdapterFactory;
private Context context;
private Gson gson;
public RxErrorHandlingCallAdapterFactory(Context context, Gson gson) {
this.context = context;
this.gson = gson;
rxJavaCallAdapterFactory = RxJava2CallAdapterFactory.create();
}
public static CallAdapter.Factory create(Context context, Gson gson) {
return new RxErrorHandlingCallAdapterFactory(context, gson);
}
@Nullable
@Override
public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
return new RxCallAdapterWrapper<>(retrofit, rxJavaCallAdapterFactory.get(returnType, annotations, retrofit));
}
private class RxCallAdapterWrapper<R> implements CallAdapter<R, Observable<R>> {
private final Retrofit retrofit;
private final CallAdapter<R, ?> wrappedCallAdapter;
public RxCallAdapterWrapper(Retrofit mRetrofit, CallAdapter<R, ?> wrapped) {
this.retrofit = mRetrofit;
this.wrappedCallAdapter = wrapped;
}
@Override
public Type responseType() {
return wrappedCallAdapter.responseType();
}
@SuppressWarnings("unchecked")
@Override
public Observable<R> adapt(@NonNull Call<R> call) {
return ((Observable<R>) wrappedCallAdapter.adapt(call))
.onErrorResumeNext(new Function<Throwable, ObservableSource<? extends R>>() {
@Override
public ObservableSource<? extends R> apply(Throwable throwable) throws Exception {
return Observable.error(convertError(throwable));
}
});
}
private ErrorThrowable convertError(final Throwable throwable) {
Error error = new Error(Error.ErrorCodes.UNKNOWN_ERROR, context.getString(com.inscreen.R.string.unknown_error));
if (throwable instanceof ConnectException) {
error = new Error(Error.ErrorCodes.SOCKET_TIMEOUT_EXCEPTION,
context.getString(com.inscreen.R.string.error_connection));
return new ErrorThrowable(throwable, error);
}
if (throwable instanceof SocketTimeoutException) {
error = new Error(Error.ErrorCodes.SOCKET_TIMEOUT_EXCEPTION,
context.getString(com.inscreen.R.string.error_socket_timeout));
return new ErrorThrowable(throwable, error);
}
if (throwable instanceof HttpException) {
HttpException exception = (HttpException) throwable;
Response response = exception.response();
if (response != null && !response.isSuccessful()) {
ResponseBody errorBody = response.errorBody();
if (errorBody != null) {
try {
error = gson.fromJson(errorBody.string(), Error.class);
} catch (JsonSyntaxException | IOException exc) {
return new ErrorThrowable(throwable, error);
}
}
}
}
return new ErrorThrowable(throwable, error);
}
}
}
当错误代码“未授权”我想自动登录。像这样的https://stackoverflow.com/a/26201962/6805851,但我不知道如何做到这一点。
@SuppressWarnings("unchecked")
@Override
public Observable<R> adapt(@NonNull Call<R> call) {
return ((Observable<R>) wrappedCallAdapter.adapt(call))
.onErrorResumeNext(new Function<Throwable, ObservableSource<? extends R>>() {
@Override
public ObservableSource<? extends R> apply(Throwable throwable) throws Exception {
if (errorrCodeNotAuthorized) {
return "AutorizationObservable".flatMap(return "OLD REQUEST OBSERVABLE");
} else {
return Observable.error(convertError(throwable));
}
}
});
}
我如何在flatMap中返回“OLD REQUEST OBSERVABLE”或如何在登录后启动旧请求?
我找到了解决方案。
if (error.isNotAuthorized()) {
UserManager userManager = Application.getAppComponent().getUserManager();
String email = userManager.getEmail();
String password = userManager.getPassword();
return userManager.getLoginObservable(email, password)
.flatMap(new Function<User, ObservableSource<R>>() {
@Override
public ObservableSource<R> apply(User user) throws Exception {
return ((Observable<R>) wrappedCallAdapter.adapt(call));
}
});