春季反应堆-如何正确引发异常?

时间:2020-11-09 12:49:48

标签: java spring reactive-programming spring-webflux project-reactor

我有此代码:

   public void createImage(Image image) {
        tokenProvider.getAccessToken()
                .flatMap(accessToken -> restCllent.decodeColour(url, accessToken.getToken())
                    .flatMap(colour -> restClient.createImage(url, accessToken.getToken())))
                .subscribe();
  }

在decodeColour函数中,我有这段代码可以调用外部服务:

    public Mono<Colour> decodeColour(String path, String token) {
        log.info("Executing GET request to {}", path);
        return webClient
            .get()
            .uri(path)
            .header(HttpHeaders.AUTHORIZATION, String.format(TOKEN_BEARER, token))
            .retrieve()
            .bodyToMono(Colour.class)
            .onErrorResume(e -> Mono.error(new RuntimeException("Error occurred during colour decoding: " + e.getMessage())));
    }

当外部服务返回例如401时,我在onErrorResume()中进行了处理, 我只想抛出RuntimeException。 在“ createImage(Image image)”函数上方的代码中,如果在decodeColour()中发生错误,如何强制代码仅抛出RuntimeException? 如果我这样离开,我会 ErrorCallbackNotImplemented:RuntimeException:颜色解码期间发生错误。 而且,如果我添加了诸如doOnError之类的错误回调,它只会处理错误,但是我想编程引发RuntimeException。对我来说,这样做很重要。

0 个答案:

没有答案