0

When making a call to my Rest API's endpoint, I am taking into account that it may end up in an exception thrown.

For example, I've got an endpoint GET /api/entity?name=${some_name}. Suppose that there's no entity of name XYZ. In that case, when someone makes a request GET /api/entity?name=XYZ, my service will throw EntityNotFoundException - a custom exception class I've made.

This is how such request looks like:

public void apiCall() {
    webClient.method(HttpMethod.GET)
                .uri(uriBuilder -> uriBuilder
                        .path("/api/entity")
                        .queryParam("name", "XYZ")
                        .build()
                ).retrieve()
                .bodyToMono(MyCustomClass.class)
                .block();
}

The above call will throw WebClientResponseException. All I really need is the underlying exception that is thrown in EntityService and is called EntityNotFoundException. How do I retrieve my custom exception?

IceMajor
  • 37
  • 5
  • Not sure what you mean by underlining exception. This is a client and it handles HTTP request/responses. Probably you mean that server throws `EntityNotFoundException` but it will be transformed into HTTP response (probably JSON) that you would need to handle in your client. Check https://stackoverflow.com/a/71650789/9068895 for details – Alex Sep 01 '23 at 04:47

0 Answers0