My application depends on a set of external Spring Interceptors which throw an exception and my service returns an HTTP 500. I would like to catch the exception, and if the stack trace matches some text, I would like to return a HTTP 400 instead. The exception thrown is of type NestedServletException
.
I have tried implementing a @ControllerAdvice
but it doesn’t seem to catch the NestedServletException
.
@ControllerAdvice
public class ErrorHandlingController {
@ExceptionHandler(NestedServletException.class)
public ResponseEntity<String> exceptionHandler(NestedServletException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
Any idea how to catch this? The stack trace looks something like:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is: The requested secret was not found.
| at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
| at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
| at javax.servlet.http.HttpServlet.service(HttpServlet.java:555)
| at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
| at javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
| at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
| at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
| at com.amazon.core.platform.runtime.FinalRequestFilter.doFilter(FinalRequestFilter.java:72)
| at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354)
| at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267)
| at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:169)
| at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
| at com.amazon.spring.platform.runtime.RequestPropagatingFilterProxy$RequestPropagatingFilterChainWrapper.doFilter(RequestPropagatingFilterProxy.java:101)