0

I have a method that when the user clicks a button, it calls another method and invokes a WebClient, etc. Basically, the WebClient throws an exception but the calling method is not capturing it.

For example, here is a shortened version:

Calling Method

    try {
        service.getMessageAsync(messageDto, results -> getUI().ifPresent(ui -> ui.access(() -> {
                .... code here ....
        })));
    } catch (RuntimeException e) {
        logger.error("RuntimeException (startEtl):");
        logger.error("\tException: {0}", e);
    }

Service Method

public void getMessageAsync(MessageDto messageDto, AsyncRestCallback<MessageDto> callback)
        throws RuntimeException {

    ... code omitted ...

    WebClient.RequestHeadersSpec<?> spec = WebClient
            .create()
            .post()
            .uri(URI)
            .body(Mono.just(messageDto), MessageDto.class)
            .headers(httpHeaders -> httpHeaders.setBasicAuth(username, password));

    spec
            .retrieve()
            .onStatus(HttpStatus::is4xxClientError, error -> Mono.error(new RuntimeException("4xx Client Error.")))
            .onStatus(HttpStatus::is5xxServerError, error -> Mono.error(new RuntimeException("5xx Server Error.")))
            .toEntityList(MessageDto.class).subscribe(result -> {

                callback.operationFinished(message.get(0));
    });
}

So, in my logs, I can clearly see the 4xx Client Error. entries. I'm not sure how they are in the logs. But I do NOT see the RuntimeException (startEtl): in the calling method.

I'm sure I've overlooked something obvious but I can't seem to find it.

So why is the calling exception handling not working as I expect? Is it because onStatus is somehow handling the exception internally?

Thanks

cbmeeks
  • 11,248
  • 22
  • 85
  • 136
  • 1
    Because it’s async. Your `subscribe` call simply throws the error away ... asynchronously. You need to call the other `subscribe` method passing in an error callback. – Boris the Spider Feb 02 '21 at 22:51
  • Ah! OK, that totally makes sense. This is what I get for working on six projects at the same time. If you want to make that an answer, I will accept. Thanks. – cbmeeks Feb 03 '21 at 13:37

0 Answers0