The difference between onErrorResume and doOnError

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.

Leave a Comment