java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
io.avaje.http.client.HttpException
- All Implemented Interfaces:
Serializable
HTTP Exception with support for converting the error response body into a bean.
Wraps an underlying HttpResponse with helper methods to get the response body as string or as a bean.
Example catching HttpException
try {
client.request()
.path("hello/saveForm")
.formParam("email", "user@foo.com")
.formParam("url", "notAValidUrl")
.POST()
.asVoid();
} catch (HttpException e) {
// obtain the statusCode from the exception ...
int statusCode = e.statusCode());
HttpResponse<?> httpResponse = e.httpResponse();
// obtain the statusCode from httpResponse ...
int statusCode = httpResponse.statusCode();
// convert error response body into a bean (typically Jackson/Gson)
final MyErrorBean errorResponse = e.bean(MyErrorBean.class);
final Map<String, String> errorMap = errorResponse.getErrors();
assertThat(errorMap.get("url")).isEqualTo("must be a valid URL");
assertThat(errorMap.get("name")).isEqualTo("must not be null");
}
- See Also:
-
Constructor Summary
ConstructorDescriptionHttpException
(int statusCode, String message) Create with status code and message.HttpException
(int statusCode, String message, Throwable cause) Create with status code, message and throwable.HttpException
(int statusCode, Throwable cause) Create with status code and throwable. -
Method Summary
Modifier and TypeMethodDescription<T> T
Return the response body content as a bean, or else null if body content doesn't exist.byte[]
Return the response body content as raw bytes, or else null if body content doesn't exist.Return the response body content as a UTF8 string, or else null if body content doesn't exist.Return the response Content-Type header.HttpResponse
<?> Return the underlying HttpResponse.boolean
Return true if the Content-Type is text/plain.int
Return the HTTP status code.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
HttpException
Create with status code and message. -
HttpException
Create with status code, message and throwable. -
HttpException
Create with status code and throwable.
-
-
Method Details
-
bean
Return the response body content as a bean, or else null if body content doesn't exist.- Parameters:
cls
- The type of bean to convert the response to- Returns:
- The response as a bean
-
bodyAsString
Return the response body content as a UTF8 string, or else null if body content doesn't exist. -
bodyAsBytes
public byte[] bodyAsBytes()Return the response body content as raw bytes, or else null if body content doesn't exist. -
statusCode
public int statusCode()Return the HTTP status code. -
httpResponse
Return the underlying HttpResponse. -
contentType
Return the response Content-Type header. -
isPlainText
public boolean isPlainText()Return true if the Content-Type is text/plain.
-