The difference between onErrorResume and doOnError

拟墨画扇 提交于 2020-12-29 12:08:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!