单个RXjava onError不起作用

时间:2018-01-28 09:57:32

标签: rx-java

我正在尝试使用rx java调用http调用。

它返回一个Single对象。

这是代码:

 service.getEvent(eventId)
    .onErrorResumeNext(exception -> Single.error(exception))
    .doOnError(throwable -> log.error(throwable.getMessage())
    .subscribe(this::handleEvent)

日志正在打印“doOnError”中预期的日志行,但也会打印堆栈跟踪:

 ERROR c.b.w.s.Service:161 - Error: Error, eventId: dummy-event

rx.exceptions.OnError
NotImplementedException: Error
    at rx.functions.Actions$NotImplemented.call(Actions.java:576)
    at rx.functions.Actions$NotImplemented.call(Actions.java:572)
    at rx.Single$11.onError(Single.java:1782)
    at rx.internal.operators.SingleDoOnEvent$SingleDoOnEventSubscriber.onError(SingleDoOnEvent.java:76)
    at rx.Single$1.call(Single.java:460)
    at rx.Single$1.call(Single.java:456)
    at rx.Single.subscribe(Single.java:1967)
    at rx.internal.operators.SingleOperatorOnErrorResumeNext$2.onError(SingleOperatorOnErrorResumeNext.java:69)
    at rx.Single$1.call(Single.java:460)
    at rx.Single$1.call(Single.java:456)
    at rx.Single.subscribe(Single.java:1967)
    at rx.internal.operators.SingleOperatorOnErrorResumeNext.call(SingleOperatorOnErrorResumeNext.java:77)
    at rx.internal.operators.SingleOperatorOnErrorResumeNext.call(SingleOperatorOnErrorResumeNext.java:23)
    at rx.Single.subscribe(Single.java:1967)
    at rx.internal.operators.SingleDoOnEvent.call(SingleDoOnEvent.java:40)
    at rx.internal.operators.SingleDoOnEvent.call(SingleDoOnEvent.java:25)
    at rx.Single.subscribe(Single.java:1967)
    at rx.Single.subscribe(Single.java:1777)
    at rx.Single.subscribe(Single.java:1747)

如何删除堆栈跟踪...还有,我确实实现了onError?

单身是正确的选择还是我应该使用Observable?

此致 IDO

1 个答案:

答案 0 :(得分:4)

您没有实现onError调用的subscribe处理程序。 doOnError只是偷看错误,但不会消费"它。将doOnError参数作为第二个参数移动到subscribe

service.getEvent(eventId)
.onErrorResumeNext(exception -> Single.error(exception))
.subscribe(this::handleEvent, throwable -> log.error(throwable.getMessage())

此外,您的onErrorResumeNext没有实际效果,因为它只是复制了它收到的同一个例外。