1

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?

akarnokd
  • 69,132
  • 14
  • 157
  • 192
Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103

1 Answers1

2

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)
akarnokd
  • 69,132
  • 14
  • 157
  • 192
  • Thank you I'll apply first asap. Why I am confused about the the second issue my data.model.kt does not have so much line 571!?!?!? – Ralf Wickum Mar 01 '22 at 10:06
  • 1
    I have no idea how Kotlin assigns code lines to its language features. Looks like some kind of class or instance initialization, i.e., lateinit with lambda, the 31st of such lambdas in the file. It triggers from a `doOnEach` so if you have only one of such, that's the point to look into. If you have more `doOnEach`, you'll have to add try-catches around their handler and manually tag each such location (Log.e) to find the culprit. – akarnokd Mar 01 '22 at 10:19