Reputation: 34527
We have a tomcat webapp which happens to throw some scary SAML authentication exception deep within crazy Spring SAML internals during a somewhat normal invalid login attempt. The exception is unhandled and user is redirected to a semi-helpful error page using
<error-page>
<exception-type>com.test.SomeException</exception-type>
<location>/error.jsp</location>
</error-page>
It works fine, except tomcat still logs that unhandled exception in the catalina.out. Is there some simple way to prevent it from polluting the log file with the strack traces (which get picked up by the log monitoring software etc) w/o restructuring everything? And what is the proper way of dealing with this anyways?
Upvotes: 1
Views: 691
Reputation: 34527
So here is what I have arrived to for now, not ideal, but sort of works:
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
doFilterInternal(servletRequest, servletResponse, filterChain);
} catch (MyVerySpecificSamlAuthException ex) {
HttpServletRequest request = (HttpServletRequest)servletRequest;
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/general_error.jsp");
request.setAttribute(RequestDispatcher.ERROR_MESSAGE, ex.getMessage());
requestDispatcher.forward(request, servletResponse);
}
}
Upvotes: 1
Reputation: 15533
You can of course handle the exceptions within Spring, you don't need to let them bubble to the handler in web.xml. See some examples for example in this article.
Upvotes: 1