Interface ErrorResponse
- All Known Implementing Classes:
AsyncRequestTimeoutException, ContentTooLargeException, ErrorResponseException, HandlerMethodValidationException, HttpMediaTypeException, HttpMediaTypeNotAcceptableException, HttpMediaTypeNotSupportedException, HttpRequestMethodNotSupportedException, InvalidApiVersionException, MaxUploadSizeExceededException, MethodArgumentNotValidException, MethodNotAllowedException, MissingApiVersionException, MissingMatrixVariableException, MissingPathVariableException, MissingRequestCookieException, MissingRequestHeaderException, MissingRequestValueException, MissingRequestValueException, MissingServletRequestParameterException, MissingServletRequestPartException, NoHandlerFoundException, NoResourceFoundException, NoResourceFoundException, NotAcceptableApiVersionException, NotAcceptableStatusException, PayloadTooLargeException, ResponseStatusException, ServerErrorException, ServerWebInputException, ServletRequestBindingException, UnsatisfiedRequestParameterException, UnsatisfiedServletRequestParameterException, UnsupportedMediaTypeStatusException, WebExchangeBindException
ProblemDetail body. Allows any
exception to expose HTTP error response information.
ErrorResponseException is a default implementation of this
interface and a convenient base class for other exceptions to use.
ErrorResponse is supported as a return value from
@ExceptionHandler methods that render directly to the response, for example,
by being marked @ResponseBody, or declared in an
@RestController or RestControllerAdvice class.
- Since:
- 6.0
- Author:
- Rossen Stoyanchev
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceBuilder for anErrorResponse.static interfaceCallback to perform an action before an RFC-9457ProblemDetailresponse is rendered. -
Method Summary
Modifier and TypeMethodDescriptionstatic ErrorResponse.Builderbuilder(Throwable ex, HttpStatusCode statusCode, String detail) Return a builder to create anErrorResponseinstance.static ErrorResponse.Builderbuilder(Throwable ex, ProblemDetail problemDetail) Variant ofbuilder(Throwable, HttpStatusCode, String)for use with a customProblemDetailinstance.static ErrorResponsecreate(Throwable ex, HttpStatusCode statusCode, String detail) Static factory method to build an instance viabuilder(Throwable, HttpStatusCode, String).getBody()Return the body for the response, formatted as an RFC 9457ProblemDetailwhosestatusshould match the response status.static StringgetDefaultDetailMessageCode(Class<?> exceptionType, @Nullable String suffix) Build a message code for the "detail" field, for the given exception type.static StringgetDefaultTitleMessageCode(Class<?> exceptionType) Build a message code for the "title" field, for the given exception type.static StringgetDefaultTypeMessageCode(Class<?> exceptionType) Build a message code for the "type" field, for the given exception type.Return arguments to use along with amessage codeto resolve the problem "detail" for this exception through aMessageSource.getDetailMessageArguments(MessageSource messageSource, Locale locale) Variant ofgetDetailMessageArguments()that uses the givenMessageSourcefor resolving the message argument values.default StringReturn a code to use to resolve the problem "detail" for this exception through aMessageSource.default HttpHeadersReturn headers to use for the response.Return the HTTP status code to use for the response.default StringReturn a code to use to resolve the problem "title" for this exception through aMessageSource.default StringReturn a code to use to resolve the problem "type" for this exception through aMessageSource.default ProblemDetailupdateAndGetBody(@Nullable MessageSource messageSource, Locale locale) Use the givenMessageSourceto resolve thetype,title, anddetailmessage codes, and then use the resolved values to update the corresponding fields ingetBody().
-
Method Details
-
getStatusCode
HttpStatusCode getStatusCode()Return the HTTP status code to use for the response. -
getHeaders
Return headers to use for the response. -
getBody
ProblemDetail getBody()Return the body for the response, formatted as an RFC 9457ProblemDetailwhosestatusshould match the response status.Note: The returned
ProblemDetailmay be updated before the response is rendered, for example, viaupdateAndGetBody(MessageSource, Locale). Therefore, implementing methods should use an instance field, and should not re-create theProblemDetailon every call, nor use a static variable. -
getTypeMessageCode
Return a code to use to resolve the problem "type" for this exception through aMessageSource. The type resolved through theMessageSourcewill be passed intoURI.create(String)and therefore must be an encoded URI String.By default this is initialized via
getDefaultTypeMessageCode(Class).- Since:
- 6.1
-
getTitleMessageCode
Return a code to use to resolve the problem "title" for this exception through aMessageSource.By default this is initialized via
getDefaultTitleMessageCode(Class). -
getDetailMessageCode
Return a code to use to resolve the problem "detail" for this exception through aMessageSource.By default this is initialized via
getDefaultDetailMessageCode(Class, String). -
getDetailMessageArguments
Return arguments to use along with amessage codeto resolve the problem "detail" for this exception through aMessageSource. The arguments are expanded into placeholders of the message value, for example, "Invalid content type {0}". -
getDetailMessageArguments
Variant ofgetDetailMessageArguments()that uses the givenMessageSourcefor resolving the message argument values.This is useful for example to expand message codes from validation errors.
The default implementation delegates to
getDetailMessageArguments(), ignoring the suppliedMessageSourceandLocale.- Parameters:
messageSource- theMessageSourceto use for the lookuplocale- theLocaleto use for the lookup
-
updateAndGetBody
Use the givenMessageSourceto resolve thetype,title, anddetailmessage codes, and then use the resolved values to update the corresponding fields ingetBody().- Parameters:
messageSource- theMessageSourceto use for the lookuplocale- theLocaleto use for the lookup
-
getDefaultTypeMessageCode
Build a message code for the "type" field, for the given exception type.- Parameters:
exceptionType- the exception type associated with the problem- Returns:
"problemDetail.type."followed by the fully qualifiedclass name- Since:
- 6.1
-
getDefaultTitleMessageCode
Build a message code for the "title" field, for the given exception type.- Parameters:
exceptionType- the exception type associated with the problem- Returns:
"problemDetail.title."followed by the fully qualifiedclass name
-
getDefaultDetailMessageCode
Build a message code for the "detail" field, for the given exception type.- Parameters:
exceptionType- the exception type associated with the problemsuffix- an optional suffix, for example, for exceptions that may have multiple error message with different arguments- Returns:
"problemDetail."followed by the fully qualifiedclass nameand an optional suffix
-
create
Static factory method to build an instance viabuilder(Throwable, HttpStatusCode, String). -
builder
Return a builder to create anErrorResponseinstance.- Parameters:
ex- the underlying exception that lead to the error response; mainly to derive default values for the detail message code and for the title message code.statusCode- the status code to set in the responsedetail- the default value for thedetailfield, unless overridden by aMessageSourcelookup withgetDetailMessageCode()
-
builder
Variant ofbuilder(Throwable, HttpStatusCode, String)for use with a customProblemDetailinstance.- Since:
- 6.1
-