Package tech.greenfield.vertx.irked
Class HttpError
- java.lang.Object
-
- java.lang.Throwable
-
- java.lang.Exception
-
- tech.greenfield.vertx.irked.HttpError
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
Accepted,BadGateway,BadRequest,Conflict,Continue,Created,ExpectationFailed,FailedDependency,Forbidden,Found,GatewayTimeout,Gone,HTTPVersionNotSupported,Imateapot,IMUsed,InsufficientStorage,InternalServerError,LengthRequired,Locked,MethodNotAllowed,MovedPermanently,MultipleChoices,MultiStatus,NetworkAuthenticationRequired,NoContent,NonAuthoritativeInformation,NotAcceptable,NotFound,NotImplemented,NotModified,OK,PartialContent,PayloadTooLarge,PaymentRequired,PermanentRedirect,PreconditionFailed,PreconditionRequired,Processing,ProxyAuthenticationRequired,RangeNotSatisfiable,RequestHeaderFieldsTooLarge,RequestTimeout,ResetContent,SeeOther,ServiceUnavailable,SwitchingProtocols,TemporaryRedirect,TooManyRequests,Unauthorized,UnavailableForLegalReasons,UnprocessableEntity,UnsupportedMediaType,UpgradeRequired,URITooLong,UseProxy,VariantAlsoNegotiates
public class HttpError extends Exception
Top class representing HTTP response headers in Irked. This class is mostly useful as a quick way to abort processing with the correct HTTP error response, as it extends Exception, but can also be used with the variousRequest.send()methods as a shorthand for callingresponse().setResponseCode(...).setStatusMessage(...)- Author:
- odeda
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classHttpError.UncheckedHttpErrorA RuntimeException wrapper for HttpError to allow it to be thrown from within non-declaring code.
-
Constructor Summary
Constructors Constructor Description HttpError(int statusCode, String statusText)Creates a new HttpError instance with the specified status code and status text.HttpError(int statusCode, String statusText, String message)Creates a new HttpError instance with the specified status code and status text and a custom error message to be serialized out in default JSON serialization.HttpError(int statusCode, String statusText, String message, Throwable throwable)Creates a new HttpError instance with the specified status code and status text and a causing exception.HttpError(int statusCode, String statusText, Throwable throwable)Creates a new HttpError instance with the specified status code and status text and a causing exception.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpErroraddHeader(String header, String value)Add a header line to be written to the response's header by Irked when this status is used to send a response.io.vertx.core.MultiMapgetHeaders()Retrieve a reference to the current set of additional headers.intgetStatusCode()Retrieve this instance's numeric HTTP status codeStringgetStatusText()Retrieve this instance's HTTP status messagebooleanisClientError()Check whether this instance represents a 4xx class HTTP responsebooleanisError()Check whether this instance represents a "error class" HTTP responsebooleanisOK()Check whether this instance represents a 2xx class HTTP responsebooleanisRedirect()Check whether this instance represents a 3xx class HTTP responsebooleanisServerError()Check whether this instance represents a 5xx class HTTP responseHttpErrorsetStatusText(String reasonText)Update the current instance's status message with a non-default HTTP status message.static HttpErrortoHttpError(io.vertx.ext.web.RoutingContext ctx)Helper method for OnFail handlers to create an appropriate HTTP error class for a failedRoutingContext, by reading the failed status code or failure exception.static HttpErrortoHttpError(Throwable t)Helper method for OnFail handlers to locate a wrapped HttpError or create an InternalServerError from an unexpected exception (whether it is wrapped or not)StringtoString()RuntimeExceptionunchecked()Helper method to make it easier to throw HTTP statuses out of lambdas.RuntimeExceptionuncheckedWrap()Alias tounchecked()static Throwableunwrap(Throwable t)UnwrapRuntimeExceptionwrappers around a logical exception (hopefully an instance of HttpError)-
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
-
-
-
-
Constructor Detail
-
HttpError
public HttpError(int statusCode, String statusText)Creates a new HttpError instance with the specified status code and status text. It is preferable and easier to use the specific class for the standard status code that you want send, so only use this constructor for non-standard custom response status codes- Parameters:
statusCode- HTTP status codestatusText- HTTP status message
-
HttpError
public HttpError(int statusCode, String statusText, String message)Creates a new HttpError instance with the specified status code and status text and a custom error message to be serialized out in default JSON serialization. It is preferable and easier to use the specific class for the standard status code that you want send, so only use this constructor for non-standard custom response status codes- Parameters:
statusCode- HTTP status codestatusText- HTTP status messagemessage- The error message to show in the body of the response
-
HttpError
public HttpError(int statusCode, String statusText, Throwable throwable)Creates a new HttpError instance with the specified status code and status text and a causing exception. The resulting exception will serialize the cause's message as the error message in default JSON serialization. It is preferable and easier to use the specific class for the standard status code that you want send, so only use this constructor for non-standard custom response status codes- Parameters:
statusCode- HTTP status codestatusText- HTTP status messagethrowable- The exception causing this status to be sent
-
HttpError
public HttpError(int statusCode, String statusText, String message, Throwable throwable)Creates a new HttpError instance with the specified status code and status text and a causing exception. The resulting exception will serialize the provided custom message as the error message in default JSON serialization, instead of the cause's message. It is preferable and easier to use the specific class for the standard status code that you want send, so only use this constructor for non-standard custom response status codes- Parameters:
statusCode- HTTP status codestatusText- HTTP status messagemessage- The error message to show in the body of the responsethrowable- The exception causing this status to be sent
-
-
Method Detail
-
getStatusCode
public int getStatusCode()
Retrieve this instance's numeric HTTP status code- Returns:
- HTTP status code
-
getStatusText
public String getStatusText()
Retrieve this instance's HTTP status message- Returns:
- HTTP status message
-
setStatusText
public HttpError setStatusText(String reasonText)
Update the current instance's status message with a non-default HTTP status message. The use of this method is not recommended and is offered only for supporting Vert.xHttpServerResponse.setStatusMessage(String)API.- Parameters:
reasonText- The non-standard HTTP status message to use- Returns:
- the same instance for use as a fluent API
-
isOK
public boolean isOK()
Check whether this instance represents a 2xx class HTTP response- Returns:
- whether the HTTP status code is between 200-299 (inclusive)
-
isRedirect
public boolean isRedirect()
Check whether this instance represents a 3xx class HTTP response- Returns:
- whether the HTTP status code is between 300-399 (inclusive)
-
isClientError
public boolean isClientError()
Check whether this instance represents a 4xx class HTTP response- Returns:
- whether the HTTP status code is between 400-499 (inclusive)
-
isServerError
public boolean isServerError()
Check whether this instance represents a 5xx class HTTP response- Returns:
- whether the HTTP status code is between 500-599 (inclusive)
-
isError
public boolean isError()
Check whether this instance represents a "error class" HTTP response- Returns:
- whether the HTTP status code is between 400-599 (inclusive)
-
addHeader
public HttpError addHeader(String header, String value)
Add a header line to be written to the response's header by Irked when this status is used to send a response.- Parameters:
header- header namevalue- header value- Returns:
- the same instance for use as a fluent API
-
getHeaders
public io.vertx.core.MultiMap getHeaders()
Retrieve a reference to the current set of additional headers.- Returns:
- the current set of additional headers
-
uncheckedWrap
public RuntimeException uncheckedWrap()
Alias tounchecked()- Returns:
- unchecked
RuntimeExceptionwrapping this status instance
-
unchecked
public RuntimeException unchecked()
Helper method to make it easier to throw HTTP statuses out of lambdas. Outside the lambda you should catch aRuntimeExceptionand useunwrap(Throwable)to get the original exception- Returns:
- unchecked
RuntimeExceptionwrapping this status instance
-
unwrap
public static Throwable unwrap(Throwable t)
UnwrapRuntimeExceptionwrappers around a logical exception (hopefully an instance of HttpError)- Parameters:
t- Throwable to unwrap- Returns:
- the first non RuntimeException found
-
toHttpError
public static HttpError toHttpError(Throwable t)
Helper method for OnFail handlers to locate a wrapped HttpError or create an InternalServerError from an unexpected exception (whether it is wrapped or not)- Parameters:
t- Throwable to be analyzed- Returns:
- the wrapped HttpError instance or a new
InternalServerErrorwrapping the real exception
-
toHttpError
public static HttpError toHttpError(io.vertx.ext.web.RoutingContext ctx)
Helper method for OnFail handlers to create an appropriate HTTP error class for a failedRoutingContext, by reading the failed status code or failure exception. IfRoutingContext.failure()returns a (possibly wrapped)HttpErrorthen that what will be returned, otherwise either anInternalServerErrorwill be returned (for an exception failure) or an appropriate default HTTP status instance according to the failed status code. Note that if theRoutingContext.failed()== false, then anOKHTTP status class instance will be returned.- Parameters:
ctx- failedRoutingContextto investigate- Returns:
- an
HttpErrorinstance representing the status of theRoutingContext
-
-