How to handle `IllegalArgumentException` in RxJava?

I am using Retrofit with RxJava for network calls and RxBinding for browsing operations. On the registration screen, when I click the "Register" button, I send information to the local server using the MyApi service.

SignupActivity.class

 mCompositeSubscription.add(RxView.clicks(mRegisterButton).debounce(300, TimeUnit.MILLISECONDS). subscribe(view -> { registerUser(); }, e -> { Timber.e(e, "RxView "); onRegistrationFailed(e.getMessage()); })); private void registerUser() { mCompositeSubscription.add(api.registerUser(mEmail, mPassword, mConfirmPassword) .subscribe(user -> { Timber.d("Received user object. Id: " + user.getUserId()); }, e -> { Timber.e(e, "registerUser() "); onRegistrationFailed(e.getMessage()); })); } 

MyApi.class

  public Observable<User> registerUser(String username, String password, String confirmPassword) { return mService.registerUser(username, password, confirmPassword) .compose(applySchedulers()); } @SuppressWarnings("unchecked") <T> Observable.Transformer<T, T> applySchedulers() { return observable -> observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } 

MyService.class

  @FormUrlEncoded @POST("users/") Observable<User> registerUser(@Path("email") String username, @Path("password") String password, @Path("password_confirmation") String confirmPassword); 

The call IllegalArgumentException with an IllegalArgumentException because I am sending incorrect information.

What is my main problem, after IllegalArgumentException I thought that RxJava would execute registerUser()#ErrorHandler() since my registerUser service call failed, but instead it calls RxView#ErrorHandler() .

How can I force / force registerUser()#ErrorHandler() take care of the exception that occurred during a network call?

+5
source share
1 answer

My bad, network call will not work with IllegalArgumentException , but the request construct itself ended with IllegalArgumentException .

 java.lang.IllegalArgumentException: URL "users/" does not contain {email}". 

Instead of using the @Field annotation to build the POST body, I mistakenly used the @Path annotation.

The correct definition is:

  @FormUrlEncoded @POST("users/") Observable<User> registerUser(@Field("email") String username, @Field("password") String password, @Field("password_confirmation") String confirmPassword); 
+2
source

Source: https://habr.com/ru/post/1235307/


All Articles