0

Error stacktrace is not printed in console for the custom exception that is annotated with @ResponseStatus

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalErrorException extends RuntimeException {

  public InternalErrorException(String message) {
    super(message);
  }

  public InternalErrorException(String message, Throwable throwable) {
    super(message, throwable);
  }
}

Throwing exception like throw new InternalErrorException("error", e), never get the stacktrace printed in the console unlesss I remove the annotation @ResponseStatus

How could I get it printed while keeping the annotation @ResponseStatus?

  • What is your question? – Andreas Jun 19 '20 at 10:16
  • How to get it printed? – Sereysopheak Eap Jun 19 '20 at 10:19
  • are you printing the stack trace? can you share you are code where you are trying to print it – Suraj Jun 19 '20 at 10:20
  • Did you read "[Exception Handling in Spring MVC](https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc)"? – Andreas Jun 19 '20 at 10:23
  • Maybe this answers your question: [Spring MVC not logging all exceptions](https://stackoverflow.com/q/11692635/5221149) – Andreas Jun 19 '20 at 10:23
  • @Andreas, then why removing the annotation works? – Sereysopheak Eap Jun 19 '20 at 10:26
  • @Suraj, the throw able object is already there in the throw clause. I'm not supposed print it. As I mentioned, I could see it if I just remove the annotation. – Sereysopheak Eap Jun 19 '20 at 10:30
  • 2
    When the code throws an exception that is unhandled, the exception percolates all the way back up to Tomcat, which then logs it. When the exception is annotated with `@ResponseStatus`, Spring intercepts the exception and converts it into a `response.sendError(...)` call to generate a standard HTTP response with the given status code, so there is no exception percolating up to Tomcat, and hence no logging is done. The Spring code *can* log the exception, if you set the `warnLogCategory` property as show in the earlier link, but is doesn't log the stacktrace. – Andreas Jun 19 '20 at 11:13
  • this is another way to handle exceptions in spring boot, https://stackoverflow.com/a/57859900/5001937 – Suraj Jun 19 '20 at 11:58

1 Answers1

0

See Annotation Type ResponseStatus API doc.

Warning: when using this annotation on an exception class, or when setting the reason attribute of this annotation, the HttpServletResponse.sendError method will be used.

With HttpServletResponse.sendError, the response is considered complete and should not be written to any further. Furthermore, the Servlet container will typically write an HTML error page therefore making the use of a reason unsuitable for REST APIs. For such cases it is preferable to use a ResponseEntity as a return type and avoid the use of @ResponseStatus altogether.

HttpServletResponse.sendError does not throw your error and I guess it is never logged because of that.

Maybe you want to implement exception handler for that exception to get it logged.

Related question

Community
  • 1
  • 1
pirho
  • 11,565
  • 12
  • 43
  • 70
  • I know about the exception handler by using other approach. I just want to find if there's a shortcut way to handle this. Something which is similar to ```server.error.include-stacktrace``` – Sereysopheak Eap Jun 19 '20 at 11:23
  • 1
    I think what Andreas says in his comment to your question answers this also quite well. You can not achieve this and the answer is in the documentation. – pirho Jun 19 '20 at 11:29