Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between onErrorResume and doOnError

In spring project reactor, what are the differences between onErrorResume and doOnError ? and when I should each of them ?

like image 942
Melad Basilius Avatar asked Sep 30 '19 12:09

Melad Basilius


People also ask

What does onErrorResume do?

You may use a onErrorResume( ) which will return another stream on elements from the point you encountered that error. Note: Using the instanceOf method, you may be able to verify if its an Exception of some expected type and hence you could handle it accordingly.


Video Answer


1 Answers

onErrorResume: Gives a fallback stream when some exception occurs happens in the upstream.

doOnError: Side-effect operator. Suppose you want to log what error happens in the upstream.

Example:

Mono.just(request)
.flatMap(this::makeHTTPGet)
.doOnError(err -> {
        log.error("Some error occurred while making the POST call",err)
    })
.onErrorResume(err -> Mono.just(getFallbackResponse()));

You see, doOnError is a side-effect operator. It's like inserting a thermometer into a water pipeline and reading the temperature. Does it affect the pipeline at all? No.

Suppose now that the pipeline breaks - the city still has to get water right? So we have a fallback pipeline that can be activate in such cases. onErrorResume does exactly that.

Note: You could also log in onErrorResume. Nothing stops you from doing that.

like image 161
Prashant Pandey Avatar answered Oct 10 '22 15:10

Prashant Pandey