1
votes

I have a WS which returns 200 with no body if successful or 420 with json in error body is unsuccessful

Return type is

Observable<Response<Void>>

For some reason in case of error with 420 code onNext(Response<Void> value) is called instead of onError(Throwable e) which is being called for any other unsuccessful requests.

Why only in this case onNext instead of onError is called? It is possible to call onError if the request didn't returned 200?

1
Try Observable<Void> instead of Observable<Response<Void>> - ULazdins
Is I use Observable<Void> I get nullpointer in onError(Throwable e) - Vadims Savjolovs

1 Answers

5
votes

That's exactly the expected behavior when using Response, any kind of status code is encapsulated as a Response object, and emitted with onNext(). while any kind of other exception (network error/ parsing error/some other configuration error) will be delivered as Exception in onError().

With the bare object apprach (Observable<T>/Observable<Void>), for any other situation other than got response from server and the response was parsed successfully, you will get onError().

in case of RxJava2, Observable<Void> is no longer valid, as null cannot be emitted (not allowed anymore with RxJava2), instead you should use Completable.

With the Response, you can wrap your one Error handler for instance for specific server error codes. (you can see an example at my answer here)

In addition, there is also Result type that encapsulate any error kind and a Response if any.