Class ResponseStatusException
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 Summary
ConstructorsConstructorDescriptionResponseStatusException(int status, String reason) ResponseStatusException(int status, String reason, Throwable cause) -
Method Summary
Methods inherited from class Throwable
addSuppressed, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
ResponseStatusException
- Parameters:
status- the status to answer with, 400 to 599reason- what to tell the caller; sent as the body
-
ResponseStatusException
-
-
Method Details
-
getStatus
public int getStatus()The status to answer with. -
getReason
What the caller is told.
-