Package org.springframework.web
Interface ErrorResponse
- All Known Implementing Classes:
AsyncRequestTimeoutException,ErrorResponseException,HttpMediaTypeException,HttpMediaTypeNotAcceptableException,HttpMediaTypeNotSupportedException,HttpRequestMethodNotSupportedException,MethodArgumentNotValidException,MethodNotAllowedException,MissingMatrixVariableException,MissingPathVariableException,MissingRequestCookieException,MissingRequestHeaderException,MissingRequestValueException,MissingRequestValueException,MissingServletRequestParameterException,MissingServletRequestPartException,NoHandlerFoundException,NotAcceptableStatusException,ResponseStatusException,ServerErrorException,ServerWebInputException,ServletRequestBindingException,UnsatisfiedRequestParameterException,UnsatisfiedServletRequestParameterException,UnsupportedMediaTypeStatusException,WebExchangeBindException
public interface ErrorResponse
Representation of a complete RFC 7807 error response including status,
headers, and an RFC 7807 formatted
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, e.g.
by being marked @ResponseBody, or declared in an
@RestController or RestControllerAdvice class.
- Since:
- 6.0
- Author:
- Rossen Stoyanchev
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic ErrorResponsecreateFor(Exception ex, HttpStatusCode status, HttpHeaders headers, String defaultDetail, String detailMessageCode, Object[] detailMessageArguments) Map the given Exception to anErrorResponse.getBody()Return the body for the response, formatted as an RFC 7807ProblemDetailwhosestatusshould match the response status.static StringgetDefaultDetailMessageCode(Class<?> exceptionType, 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.default Object[]Return arguments to use along with amessage codeto resolve the problem "detail" for this exception through aMessageSource.default Object[]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 "detail" for this exception through aMessageSource.default ProblemDetailupdateAndGetBody(MessageSource messageSource, Locale locale) Resolve thedetailMessageCodeand thetitleCodethrough the givenMessageSource, and if found, update the "detail" and "title!" fields respectively.
-
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 7807ProblemDetailwhosestatusshould match the response status. -
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, e.g. "Invalid content type {0}". -
getDetailMessageArguments
Variant ofgetDetailMessageArguments()that uses the givenMessageSourcefor resolving the message argument values. This is useful for example to message codes from validation errors. -
getTitleMessageCode
Return a code to use to resolve the problem "detail" for this exception through aMessageSource.By default this is initialized via
getDefaultDetailMessageCode(Class, String). -
updateAndGetBody
Resolve thedetailMessageCodeand thetitleCodethrough the givenMessageSource, and if found, update the "detail" and "title!" fields respectively.- Parameters:
messageSource- theMessageSourceto use for the lookuplocale- theLocaleto use for the lookup
-
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, e.g. for exceptions that may have multiple error message with different arguments.- Returns:
"problemDetail."followed by the fullclass name
-
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 fullclass name
-
createFor
static ErrorResponse createFor(Exception ex, HttpStatusCode status, @Nullable HttpHeaders headers, String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments) Map the given Exception to anErrorResponse.- Parameters:
ex- the Exception, mostly to derive message codes, if not providedstatus- the response status to useheaders- optional headers to add to the responsedefaultDetail- default value for the "detail" fielddetailMessageCode- the code to use to look up the "detail" field through aMessageSource, falling back ongetDefaultDetailMessageCode(Class, String)detailMessageArguments- the arguments to go with the detailMessageCode- Returns:
- the created
ErrorResponseinstance
-