Exception Handling Reactive

梦想与她 提交于 2019-12-22 08:24:22

问题


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

  • 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)

Example scenario below:

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());
}

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/50397467/exception-handling-reactive

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