Available doOnError{} did not solve The exception not handled due to missing onError handler in the subscribe()

2.4k views Asked by At

I have a PublishSubject:

subjectA = PublishSubject.create()

whoch is then operated similar to:

  subjectA 
    .flatMap {
        //..
    }
    .flatMapUntil({ it }) {
        //..
    }
    .observeOn(AndroidSchedulers.mainThread())
    .filter { it.isFilter }
    .doOnNext {
        //..
    }
    .doOnError { e->
        Log.d("TAG", "doOnError ${e.localizedMessage}")
    }
    .takeUntil(disposeComposable)
    .subscribe()

Thinking that above code created following log output:

RX global error io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | java.util.NoSuchElementException: Collection contains no element matching the predicate. at io.reactivex.rxjava3.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:718) at io.reactivex.rxjava3.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:715) at io.reactivex.rxjava3.internal.observers.LambdaObserver.onError(LambdaObserver.java:77) at io.reactivex.rxjava3.internal.util.AtomicThrowable.tryTerminateConsumer(AtomicThrowable.java:110) at io.reactivex.rxjava3.internal.util.HalfSerializer.onError(HalfSerializer.java:118) at io.reactivex.rxjava3.internal.operators.observable.ObservableTakeUntil$TakeUntilMainObserver.onError(ObservableTakeUntil.java:85) at io.reactivex.rxjava3.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onError(ObservableDoOnEach.java:117) at io.reactivex.rxjava3.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(ObservableDoOnEach.java:97) at io.reactivex.rxjava3.internal.operators.observable.ObservableFilter$FilterObserver.onNext(ObservableFilter.java:52) at io.reactivex.rxjava3.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:202) at io.reactivex.rxjava3.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:256) at io.reactivex.rxjava3.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:123) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.util.NoSuchElementException: Collection contains no element matching the predicate. at com.example.app.DataModel.initialize$lambda-31(data.model.kt:571) at com.example.app.DataModel.$r8$lambda$9iWq6yMOxbhDAuxg-6-Wk1ZnNzk(Unknown Source:0) at com.example.app.DataModel$$ExternalSyntheticLambda11.accept(Unknown Source:4) at io.reactivex.rxjava3.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(ObservableDoOnEach.java:93)

Error message says, I do not have implemented an onError() method call: The exception was not handled due to missing onError handler in the subscribe() method call.

But I obviously added a doOnError{}. Further the localizedMessage on the above code tells:

Collection contains no element matching the predicate.

Wha is wrong here?

1

There are 1 answers

2
akarnokd On BEST ANSWER

As the error message indicates, you don't have an error handler in the subscribe method:

.doOnError { e->
    Log.d("TAG", "doOnError ${e.localizedMessage}")
}
.takeUntil(disposeComposable)
.subscribe() // <------------------------------------------------

doOnError is a different method and is not an error handler, only a peek into an error in the chain.

Consequently, you'll have to put a handler the right place:

.doOnError { e->
    Log.d("TAG", "doOnError ${e.localizedMessage}")
}
.takeUntil(disposeComposable)
.subscribe(
   { value -> Log.d("TAG", "onNext ${value}") },
   { e -> Log.d("TAG", "onError ${e.localizedMessage}") }
)

Collection contains no element matching the predicate.

Check what happens here:

at com.example.app.DataModel.initialize$lambda-31(data.model.kt:571)