0

I'm using a Spring 5/Spring boot 2.1 REST service application. When our application throws an exception, the response that the client receives by default is this nice JSON string:

{
  "timestamp": "2019-03-30T18:18:27Z",
  "status": 500,
  "error": "Internal Server Error",
  "message": "No message available",
  "path": "/api/test/"
}

I was wondering which part of Spring (or the underlying Tomcat) is responsible for creating this json message, and when does it occur? I already checked the different HandlerExceptionResolvers (as described here) and don't see it (source code of one is here).

I also implemented a webfilter in the application and when reading out the response body (using the ConcentCachedResponseWrapper to prevent the typical issue of streams only being readable once as decribed here), the response body is always empty.

So that makes me think this json is being created and set on the response AFTER the exception handlers have handled, and AFTER the filters have ran (or at least, my own filter which I didn't specify a special order for).

user1884155
  • 3,616
  • 4
  • 55
  • 108
  • https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/api/org/springframework/boot/autoconfigure/web/BasicErrorController.html BasicErrorController does it. If you have added ControllerAdvice then It also handles the exception, but if exception is still uncaught by ControllerAdvice, then BasicErrorController comes into the picture – Hemant Patel Mar 30 '19 at 18:36
  • @PratapiHemantPatel Thanks. Is it documented somewhere how and when spring decides to call this BasicErrorController? In my filter class, I did a try/catch and the catch block didn't trigger (even though the response status was 400). So I conclude the "exception" was already caught/handled somewhere else BEFORE my Filter class finishes its work. But this BasicErrorController seems to be called AFTER my Filter. So when does it run? – user1884155 Mar 30 '19 at 19:04
  • I didn't find any official document, but this question might help you https://stackoverflow.com/questions/34595605/how-to-manage-exceptions-thrown-in-filters-in-spring. In the flow, if exceptions are thrown from controller, it is handled by controller advice (if any), then filters, then BasicErrorController (/error endpoint). – Hemant Patel Mar 30 '19 at 19:10

0 Answers0