Class HttpException

  • All Implemented Interfaces:

    public class HttpException
    extends RuntimeException
    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 {
             .formParam("email", "")
             .formParam("url", "notAValidUrl")
         } 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:
    Serialized Form
    • Constructor Detail

      • HttpException

        public HttpException​(int statusCode,
                             String message)
        Create with status code and message.
      • HttpException

        public HttpException​(int statusCode,
                             String message,
                             Throwable cause)
        Create with status code, message and throwable.
      • HttpException

        public HttpException​(int statusCode,
                             Throwable cause)
        Create with status code and throwable.
    • Method Detail

      • bean

        public <T> T bean​(Class<T> cls)
        Return the response body content as a bean
        cls - The type of bean to convert the response to
        The response as a bean
      • bodyAsString

        public String bodyAsString()
        Return the response body content as a UTF8 string.
      • bodyAsBytes

        public byte[] bodyAsBytes()
        Return the response body content as raw bytes.
      • statusCode

        public int statusCode()
        Return the HTTP status code.
      • httpResponse

        public HttpResponse<?> httpResponse()
        Return the underlying HttpResponse.