2017-12-17 10 views
1

次のエラー処理コードがあります。このコードはエラーを処理するのに最適ですが、私は "not authorized"エラーを処理したいです。エラーコードは、私は自動的にログインする「権限がありません」retrofit2 rxjava2ネットワーク例外とグローバル認証を処理する

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"を返すか、ログイン後に古いリクエストを開始するにはどうすればよいですか?

答えて

0

解決策が見つかりました。

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)); 
                } 
               }); 
関連する問題