Class ErrorResponseException

All Implemented Interfaces:
Serializable, ErrorResponse
Direct Known Subclasses:
ResponseStatusException

public class ErrorResponseException extends NestedRuntimeException implements ErrorResponse
RuntimeException that implements ErrorResponse to expose an HTTP status, response headers, and a body formatted as an RFC 7808 ProblemDetail.

The exception can be used as is, or it can be extended as a more specific exception that populates the type or detail fields, or potentially adds other non-standard fields.

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

    • ErrorResponseException

      public ErrorResponseException(HttpStatus status)
      Constructor with a well-known HttpStatus.
    • ErrorResponseException

      public ErrorResponseException(HttpStatus status, @Nullable Throwable cause)
      Constructor with a well-known HttpStatus and an optional cause.
    • ErrorResponseException

      public ErrorResponseException(int status, @Nullable Throwable cause)
      Constructor that accepts any status value, possibly not resolvable as an HttpStatus enum, and an optional cause.
    • ErrorResponseException

      public ErrorResponseException(int status, ProblemDetail body, @Nullable Throwable cause)
      Constructor with a given ProblemDetail instance, possibly a subclass of ProblemDetail with extended fields.
  • Method Details

    • getRawStatusCode

      public int getRawStatusCode()
      Description copied from interface: ErrorResponse
      Return the HTTP status value for the response, potentially non-standard and not resolvable via HttpStatus.
      Specified by:
      getRawStatusCode in interface ErrorResponse
    • getHeaders

      public HttpHeaders getHeaders()
      Description copied from interface: ErrorResponse
      Return headers to use for the response.
      Specified by:
      getHeaders in interface ErrorResponse
    • setType

      public void setType(URI type)
      Set the type field of the response body.
      Parameters:
      type - the problem type
    • setTitle

      public void setTitle(@Nullable String title)
      Set the title field of the response body.
      Parameters:
      title - the problem title
    • setDetail

      public void setDetail(@Nullable String detail)
      Set the detail field of the response body.
      Parameters:
      detail - the problem detail
    • setInstance

      public void setInstance(@Nullable URI instance)
      Set the instance field of the response body.
      Parameters:
      instance - the problem instance
    • getBody

      public final ProblemDetail getBody()
      Return the body for the response. To customize the body content, use:

      By default, the status field of ProblemDetail is initialized from the status provided to the constructor, which in turn may also initialize the title field from the status reason phrase, if the status is well-known. The instance field, if not set, is initialized from the request path when a ProblemDetail is returned from an @ExceptionHandler method.

      Specified by:
      getBody in interface ErrorResponse
    • getMessage

      public String getMessage()
      Description copied from class: NestedRuntimeException
      Return the detail message, including the message from the nested exception if there is one.
      Overrides:
      getMessage in class NestedRuntimeException