Class ResponseStatusException


public class ResponseStatusException extends RuntimeException

Thrown by a handler to answer a request with a status of its choosing.

A handler that returns a value has no other way to say "409, that e-mail is taken": its return type is the body of a success. That is every method of a server interface generated from a shared @RestClient contract, and any controller method that returns its own type and not a Response.

if (taken(email)) {
    throw new ResponseStatusException(409, "That e-mail already has an account");
}

The reason is the body, as plain text, so it is what the caller reads: keep it free of anything the caller should not learn. A status below 500 is an answer and is neither logged nor recorded as a failed request; 500 and above is reported as any other failure of the handler is, and answered with the status given.

Unchecked, as Spring's is, and for the same reason: it has to pass through methods whose signatures were written without it. Thrown inside a @Transactional method it rolls the transaction back, as every unchecked exception does.

  • Constructor Details

    • ResponseStatusException

      public ResponseStatusException(int status, String reason)
      Parameters:
      status - the status to answer with, 400 to 599
      reason - what to tell the caller; sent as the body
    • ResponseStatusException

      public ResponseStatusException(int status, String reason, Throwable cause)
      Parameters:
      status - the status to answer with, 400 to 599
      reason - what to tell the caller; sent as the body
      cause - what led to it, for the log; never sent
  • Method Details

    • getStatus

      public int getStatus()
      The status to answer with.
    • getReason

      public String getReason()
      What the caller is told.