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 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 7807 ProblemDetail whose status should match the response status.
    • 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

      @Nullable default Object[] 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, e.g. "Invalid content type {0}".
    • getDetailMessageArguments

      @Nullable default Object[] 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 message codes from validation errors.
    • getTitleMessageCode

      default String getTitleMessageCode()
      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).

    • updateAndGetBody

      default ProblemDetail updateAndGetBody(@Nullable MessageSource messageSource, Locale locale)
      Resolve the detailMessageCode and the titleCode through the given MessageSource, and if found, update the "detail" and "title!" fields respectively.
      Parameters:
      messageSource - the MessageSource to use for the lookup
      locale - the Locale to use for the lookup
    • getDefaultDetailMessageCode

      static String getDefaultDetailMessageCode(Class<?> exceptionType, @Nullable String suffix)
      Build a message code for the "detail" field, for the given exception type.
      Parameters:
      exceptionType - the exception type associated with the problem
      suffix - an optional suffix, e.g. for exceptions that may have multiple error message with different arguments.
      Returns:
      "problemDetail." followed by the full class name
    • getDefaultTitleMessageCode

      static String getDefaultTitleMessageCode(Class<?> exceptionType)
      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 full class name
    • create

      static ErrorResponse create(Throwable ex, HttpStatusCode statusCode, String detail)
      Static factory method to build an instance via builder(Throwable, HttpStatusCode, String).
    • builder

      static ErrorResponse.Builder builder(Throwable ex, HttpStatusCode statusCode, String detail)
      Return a builder to create an ErrorResponse instance.
      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 the response to
      detail - the default value for the detail field, unless overridden by a MessageSource lookup with getDetailMessageCode()