How can I throw error in onNext() with RxJava

Rox :
.subscribe(
    new Action1<Response>() {
        @Override
        public void call(Response response) {
            if (response.isSuccess())
            //handle success
            else
            //throw an Throwable(reponse.getMessage())
        }
    },
    new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            //handle Throwable throw from onNext();
        }
    }
);

I don't wanna handle (!response.isSuccess()) in onNext(). How can I throw it to onError() and handle with other throwable together?

Tassos Bassoukos :

If FailureException extends RuntimeException, then

.doOnNext(response -> {
  if(!response.isSuccess())
    throw new FailureException(response.getMessage());
})
.subscribe(
    item  -> { /* handle success */ },
    error -> { /* handle failure */ }
);

This works best if you throw the exception as early as possible, as then you can do retries, alternative responses etc. easily.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=451311&siteId=1
Recommended