反应式异常处理

Fla*_*ski 5 project-reactor spring-webflux

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

  • 是否有一个响应式对象扩展Throwable了一个 Mono/Flux 对象,因此抛出的错误将等待主体被考虑?
  • 或者有没有办法在现有的 Mono 对象上添加某种“标志”,使其立即失败(以规避要求Throwable

下面的示例场景:

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

private someMethod() {
    webClient.get().retrieve().onStatus(HttpStatus::isError, this::errorHandler)
}
Run Code Online (Sandbox Code Playgroud)
private Mono<? extends Throwable> errorHandler(ClientResponse cr) {
    Mono<String> body = cr.body(BodyExtractors.toMono(String.class));

        ...<do something here>...

    return Mono.error(new WhateverException());
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Fla*_*ski 3

return body.flatMap(str -> Mono.error(new WhateverException(str)));
Run Code Online (Sandbox Code Playgroud)