Ido Barash
Ido Barash

Reputation: 5122

RXjava onError on single does not work

I am trying to invoke a http call using rx java.

it returns a Single object.

This is the code:

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

The log is printing the log line expected in the "doOnError" but also prints the stacktrace:

 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)

How can I remove the stacktrace... also, I did implement onError?

Is single the right choise or should I use Observable?

Regards, Ido

Upvotes: 1

Views: 3566

Answers (1)

akarnokd
akarnokd

Reputation: 69997

You did not implement the onError handler of the subscribe call. doOnError just peeks at the error but otherwise doesn't "consume" it. Move the doOnError argument as the second argument to subscribe:

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

Also your onErrorResumeNext has no practical effect as it just replicates the same exception it received.

Upvotes: 5

Related Questions