Closed snejokeee closed 3 weeks ago
Thank you for this contribution. Unfortunately, as a project stewarded by Broadcom, we are unable to accept contributions from Russian sources due to Broadcom export restrictions at this time. Thanks for your continued use of Spring.
Hello everyone. This is my first contribution, so please don't blame me too much. So, my use case is to develop an internal library for other teams in my company to standardize the approach to exception handling.
In a spring-framework 5.*, we used
WebClient
as the default way to consume the internal microservices REST interfaces.The purpose of my library is to provide a predefined
@ControllerAdvice
bean with@ExceptionHandler(WebClientRequestException.class)
and@ExceptionHandler(WebClientResponseException.class)
methods that handle exceptions and return@ResponseEntity<ErrorDescription>
(ErrorDescription
is the default template for all error responses used throughout our company). In those processing methods, i can get an instance oforg.springframework.http.Http.HttpMethod
andURI
(forWebClientRequestException
) and even an entireorg.springframework.http.Http.HttpRequest
(forWebClientResponseException
) corresponding to the current exception. This is very useful just to build a message for response, like this:Now to the point. We are now trying to upgrade to spring-framewrok 6.* and use RestClient, and I need to refine the library to keep the same approach to error handling. But I'm stuck on a problem with getting the request information when processing a
RestClientException
. I asked this question on stackowerflow, but it seems that the standardStatusHandler
implementation forRestClient
avoids therequest
parameter and default error handling does not useHttpMethod
andURI
in case ofRestClient
(i dont know how it works withRestTemplate
).After a bit of research, I realized that I could make a custom interceptor or/and customizer for
RestClient.Builder
to create exceptions myself and put all the necessary information into it, but I think that the request information is very useful in context of error handling, and it can be out-of-the-box like withWebClient
.I am grateful to everyone who has read this long text. Thank you.