7

So I have the following piece of code to do a GET to a remote machine:

webClient.get() .uri(myUri) .accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(String.class) .subscribe(text -> { LOG.info(text); }); 

I get this exception, no problem, I'm expecting it, but it's really hard to find any documentation how to handle these errors:

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.net.UnknownHostException 

1 Answer 1

9

To handle these exceptions you need to add the following, adapt it to your case (in my case if I get an unkownHostException I simply log a warning that the requested service is not present:

webClient.get() .uri(myUri) .accept(MediaType.APPLICATION_JSON) .retrieve() .bodyToMono(String.class) .onErrorResume(e -> { if (e instanceof UnknownHostException) { LOG.warn("Failed to get myStuff, desired service not present"); } else { LOG.error("Failed to get myStuff"); } return Mono.just("Encountered an exception"); }) .subscribe(text -> { LOG.info(text); }); 

You handle the error, and send something to the next step. I really wish there was a way to stop there and not pass anything down the pipe.

Sign up to request clarification or add additional context in comments.

2 Comments

You can always resume with Mono.empty() (instead of Mono.just("Encountered an exception")) and hence not pass anything down the pipe.
Returning Mono.empty() is the way to go, since it produces a Mono typed just as you need, but that completes without emitting any event, so the pipe stops there, which is what yo need in most cases like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.