Affects: spring-webflux : 5.3.23 Spring Boot: 2.7.5 netty : 4.1.84.Final reactor-netty-core : 1.0.22 Java VM : OpenJDK 64-Bit Server VM Java VM version : 17.0.4.1+1-LTS
Requirement : Response with the custom error message in case of 400 bad requests. "Steps to reproduce": use unsafe characters e.g % { [ etc. in the request URL RFC1738 example: http://localhost:8080/api/book?name=action{book} source code: create a sample project through https://start.spring.io/ and take the latest stable version
I tried the below option but was unable to catch the global exception. option-1:
@Component
@Order(-2)
public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHandler
Option-2: unable to extend ReactorHttpHandlerAdapter to override custom error message
catch (URISyntaxException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to get request URI: " + ex.getMessage());
}
reactorResponse.status(HttpResponseStatus.BAD_REQUEST);
return Mono.empty();
}
How to override empty response body with custom error message?
Comment From: rstoyanchev
The error happens pretty early, and we fail to even create a ServerHttpRequest
. So this is more of an error at the level of the underlying server adapter. The best I can suggest is decorating the HttpHandler
and handling the error signal:
WebHttpHandlerBuilder.applicationContext(context)
.httpHandlerDecorator(handler ->
(request, response) -> handler.handle(request, response).onErrorResume(ex -> {}))
.build();