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