可用的 doOnError{} 没有解决 The exception not handled due to missing onError handler in the subscribe()

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

我有一个 PublishSubject:

主题 A = PublishSubject.create()

然后操作类似于:

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

认为上面的代码创建了以下日志输出:

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$lambdaiWq6yMOxbhDAuxg-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)

错误消息说,我没有实现 onError() 方法调用:由于在 subscribe() 方法调用中缺少 onError 处理程序,因此未处理异常。

但是我明明加了一个doOnError{}。此外,上述代码中的 localizedMessage 告诉:

Collection contains no element matching the predicate.

这里有什么问题?

如错误消息所示,您在 subscribe 方法中没有错误处理程序:

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

doOnError 是一种不同的方法,不是错误处理程序,只是对链中错误的一瞥。

因此,您必须将处理程序放在正确的位置:

.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.

检查这里发生了什么:

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