Class ProblemDetailExceptionHandler

java.lang.Object
org.springframework.web.method.annotation.ProblemDetailExceptionHandler

public final class ProblemDetailExceptionHandler extends Object
Last-resort exception handler that renders any exception as an RFC 9457 problem detail.

This handler is meant to be invoked by the exception handler method infrastructure after all application @ExceptionHandler methods, local or declared in a @ControllerAdvice, so that the returned ErrorResponse is rendered like any other handler method return value. It is not meant to be declared as a bean by applications.

Exceptions are translated as follows:

In all cases, the type, title and detail of the problem detail can be customized through the MessageSource, see ErrorResponse.updateAndGetBody(MessageSource, Locale).

Unexpected exceptions, that is, 5xx server errors that do not come from an ErrorResponse, are logged at ERROR level; other exceptions are logged at DEBUG level.

Since:
7.1
Author:
Brian Clozel
  • Constructor Details

    • ProblemDetailExceptionHandler

      public ProblemDetailExceptionHandler(@Nullable MessageSource messageSource)
      Create a new instance.
      Parameters:
      messageSource - the MessageSource to use to resolve the type, title and detail of problem details, and @ResponseStatus reasons, if any
  • Method Details

    • handleException

      @ExceptionHandler public @Nullable ErrorResponse handleException(Exception ex, Locale locale)
      Handle the given exception and return the ErrorResponse to render.
      Parameters:
      ex - the exception to handle
      locale - the locale to use for MessageSource lookups
      Returns:
      the error response, or null if no response should be rendered