Class ProblemDetailExceptionHandler
java.lang.Object
org.springframework.web.method.annotation.ProblemDetailExceptionHandler
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:
AsyncRequestNotUsableException: no response, since the response is not usable.ErrorResponseexceptions are used as is.- Exceptions that look like a client disconnect, as determined by
DisconnectedClientHelper, result in a 500 status, in case the connection issue is with another remote host rather than with the client. ResponseStatusExceptionand exceptions annotated with@ResponseStatus, also as the cause of the given exception, result in the declared status. The reason is used as the detail, resolved through theMessageSourceif possible.TypeMismatchException,ConversionNotSupportedException,HttpMessageNotReadableException,HttpMessageNotWritableExceptionandMethodValidationExceptionresult in the same status and detail as withResponseEntityExceptionHandler.- Any other exception results in a 500 status without detail, so that the exception message is never exposed.
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 Summary
ConstructorsConstructorDescriptionProblemDetailExceptionHandler(@Nullable MessageSource messageSource) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionhandleException(Exception ex, Locale locale) Handle the given exception and return theErrorResponseto render.
-
Constructor Details
-
ProblemDetailExceptionHandler
Create a new instance.- Parameters:
messageSource- theMessageSourceto use to resolve the type, title and detail of problem details, and@ResponseStatusreasons, if any
-
-
Method Details
-
handleException
Handle the given exception and return theErrorResponseto render.- Parameters:
ex- the exception to handlelocale- the locale to use forMessageSourcelookups- Returns:
- the error response, or
nullif no response should be rendered
-