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

public interface ErrorResponse
Representation of a complete RFC 9457 error response including status, headers, and an RFC 9457 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, for example, by being marked @ResponseBody, or declared in an @RestController or RestControllerAdvice class.

Since:
6.0
Author:
Rossen Stoyanchev
See Also:
  • Method Details

    • getStatusCode

      HttpStatusCode getStatusCode()
      Return the HTTP status code to use for the response.
    • getHeaders

      default HttpHeaders getHeaders()
      Return headers to use for the response.
    • getBody

      ProblemDetail getBody()
      Return the body for the response, formatted as an RFC 9457 ProblemDetail whose status should match the response status.

      Note: The returned ProblemDetail may be updated before the response is rendered, for example, via updateAndGetBody(MessageSource, Locale). Therefore, implementing methods should use an instance field, and should not re-create the ProblemDetail on every call, nor use a static variable.

    • getTypeMessageCode

      default String getTypeMessageCode()
      Return a code to use to resolve the problem "type" for this exception through a MessageSource. The type resolved through the MessageSource will be passed into URI.create(String) and therefore must be an encoded URI String.

      By default this is initialized via getDefaultTypeMessageCode(Class).

      Since:
      6.1
    • getTitleMessageCode

      default String getTitleMessageCode()
      Return a code to use to resolve the problem "title" for this exception through a MessageSource.

      By default this is initialized via getDefaultTitleMessageCode(Class).

    • getDetailMessageCode

      default String getDetailMessageCode()
      Return a code to use to resolve the problem "detail" for this exception through a MessageSource.

      By default this is initialized via getDefaultDetailMessageCode(Class, String).

    • getDetailMessageArguments

      default Object @Nullable [] getDetailMessageArguments()
      Return arguments to use along with a message code to resolve the problem "detail" for this exception through a MessageSource. The arguments are expanded into placeholders of the message value, for example, "Invalid content type {0}".
    • getDetailMessageArguments

      default Object @Nullable [] getDetailMessageArguments(MessageSource messageSource, Locale locale)
      Variant of getDetailMessageArguments() that uses the given MessageSource for 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 supplied MessageSource and Locale.

      Parameters:
      messageSource - the MessageSource to use for the lookup
      locale - the Locale to use for the lookup
    • updateAndGetBody

      default ProblemDetail updateAndGetBody(@Nullable