-
Notifications
You must be signed in to change notification settings - Fork 41.1k
No easy way to share response customisation logic for both sendError and exception handling #7653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
NoHandlerFoundException
I believe that the property already behaves as specified. If there is no handler available to handle a particular request, an exception will be thrown in all cases. The problem is that, by default,
It's a subtle detail, but that's already the case. There's no exception involved here so there's nothing for your exception handler to handle. I think what you're looking for is a static resource handler that will throw an exception if it can't find a resource for the request. |
The problem that's described here can be generalised to anywhere that |
One problem is that |
Is there any way to handle the problem with static resources and throw-exception-if-no-handler-found ? |
Is there any update on this? |
@wilkinsona Any update on this. It seems this error is causing another issue. #29919 and #2001 when spring.web.resources.add-mappings=false
spring.mvc.throw-exception-if-no-handler-found=true |
No, sorry. We work in the open so any updates will appear in this issue when we have them. |
This issue may be resolved by the changes in spring-projects/spring-framework#29491. As of 6.1, the handler for static resources will raise |
Thanks, @rstoyanchev. I've tried updating the original test project to Spring Boot 3.2.0-SNAPSHOT and Spring Framework 6.1.0-SNAPSHOT but I couldn't get exception handling for protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
if (handler == null) {
return super.shouldApplyTo(request, null);
}
else if (handler instanceof HandlerMethod handlerMethod) {
handler = handlerMethod.getBean();
return super.shouldApplyTo(request, handler);
}
else if (hasGlobalExceptionHandlers() && hasHandlerMappings()) {
return super.shouldApplyTo(request, handler);
}
else {
return false;
}
} The
This means that the test project's |
Our need is to map all exceptions to a predefined error response model that we return to clients using our REST services. We are using Spring Boot 1.3.8 but I tried this also with Spring Boot 1.4.2 which comes alongside the Spring IO Platform Athens-SR1 release.
Currently, there is no consistent way to handle Spring MVC specific exceptions. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-error-handling discusses about handling exceptions via
ErrorController
,ErrorAttributes
and alternatively using Spring MVC's@ExceptionHandler
s and maybe extendingResponseEntityExceptionHandler
which is taking care of the Spring MVC specific exceptions and their mapping.After many different solutions I ended up extending
ResponseEntityExceptionHandler
but the issue is that every other exception exceptNoHandlerFoundException
can be handled. As suggested, one can define a property for this to change the default behavior ofDispatcherServlet
However, this works only if you disable the default resource handling by defining
I spotted this from #3980 (comment) and it is logic that is not mentioned in the Spring Boot's reference documentation. However, this is not an option if you have static resources. We for example expose our API definition using Swagger and that stops working if you disable the default resource mapping. You might be able to manually map the static resources but I would consider that a workaround.
In my opinion, the most sensible way would be to let
spring.mvc.throw-exception-if-no-handler-found=true
to throwNoHandlerFoundException
in all cases if there is handler specified. That's what the property tells anyways. If this is not possible, I hope that there would be some other way than to define your ownErrorController
orErrorAttributes
beans as they don't get enough information for you to map exceptions because the original request is instead a call to/error
path. And in addition, having multiple components responsible for the error handling logic doesn't sound a great idea.Here is an application to verify this behavior:
The main application with a REST controller exposing some test endpoints
The custom exception handler:
Tests:
testNoHandlerFoundException()
fails but other pass as expected even though I havespring.mvc.throw-exception-if-no-handler-found
set astrue
.As a summary, it would be really great if it would be possible to handle all exceptions in a consistent manner for example by extending the
ResponseEntityExceptionHandler
and adding your own exception handlers there to complement the Spring MVC exception handlers coming from the base class.Edit: Attaching a test project for your convenience: error-handling-test.zip
The text was updated successfully, but these errors were encountered: