获取Mono/Flux错误处理程序中的链式数据

huangapple go评论50阅读模式
英文:

retrieving chain data in Mono/Flux error handlers

问题

I would like to be able to use chain data in an error handler. Condensed my issue looks like this:

request.bodyToMono(MyRequest.class)
.map(dosomestuff)
.onErrorResume(dosomeerrorhandling)

request is a ServerRequest. In the request message is a requestId field. I don't know the requestId until the message has been extracted. The message is then processed (dosomestuff) during which an exception may occur. That will be "caught" by onErrorResume and at that point I'd like to know the requestId.

I don't think I can put the requestId into the Mono context as it's not available until after the Mono has started. I could write a custom exception and place the requestId in that, but I'm concerned the extra exception handling will get messy.

Any other suggestions appreciated. Perhaps I'm just not thinking about the problem in the right way? - it's been a long day.

英文:

I would like to be able to use chain data in an error handler. Condensed my issue looks like this:

request.bodyToMono(MyRequest.class)
.map(dosomestuff)
.onErrorResume(dosomeerrorhandling)

request is a ServerRequest. In the request message is a requestId field. I don't know the requestId until the message has been extracted. The message is then processed (dosomestuff) during which an exception may occur. That will be "caught" by onErrorResume and at that point I'd like to know the requestId.

I don't think I can put the requestId into the Mono context as it's not available until after the Mono has started. I could write a custom exception and place the requestId and that, but I'm concerned the extra exception handling will get messy.

Any other suggestions appreciated. Perhaps I'm just not thinking about the problem in the right way? - it's been a long day.

答案1

得分: 1

A possible approach:

    request.bodyToMono(MyRequest.class).flatMap(request -> Mono.just(request)
            .map()
            // whatever
            .onErrorResume(/* any lambda, the request is available there */)
    );
英文:

A possible approach:

    request.bodyToMono(MyRequest.class).flatMap(request -> Mono.just(request)
            .map()
            // whatever
            .onErrorResume(/* any lambda, the request is available there */)
    );

huangapple
  • 本文由 发表于 2023年7月6日 17:52:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627587.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定