问题
In spring project reactor, what are the differences between onErrorResume
and doOnError
? and when I should each of them ?
回答1:
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.
来源:https://stackoverflow.com/questions/58167842/the-difference-between-onerrorresume-and-doonerror