异常处理反应式 [英] Exception Handling Reactive

查看:50
本文介绍了异常处理反应式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Mono.error() 但附加请求返回的正文中的信息?

How does one use a Mono.error(<Throwable>) but attach information from the body returned from a request?

  • 是否有一个响应式对象扩展了 Throwable 并接受 Mono/Flux 对象,因此抛出的错误将等待主体被解释?
  • 或者有没有办法在现有的 Mono 对象上添加某种标志"以使其立即失败(规避对 Throwable 的要求)
  • Is there a reactive object that extends Throwable that takes a Mono/Flux object, so the error being thrown will wait for the body to be accounted for?
  • Or is there a way to add some sort of 'flag' onto an existing Mono object to make it fail instantly (to circumvent the requirement to be Throwable)

以下示例场景:

import org.springframework.web.reactive.function.client.WebClient;

private someMethod() {
    webClient.get().retrieve().onStatus(HttpStatus::isError, this::errorHandler)
}

<小时>

private Mono<? extends Throwable> errorHandler(ClientResponse cr) {
    Mono<String> body = cr.body(BodyExtractors.toMono(String.class));

        ...<do something here>...

    return Mono.error(new WhateverException());
}

谢谢

推荐答案

return body.flatMap(str -> Mono.error(new WhateverException(str)));

这篇关于异常处理反应式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆