org.springframework.web.client

## Class DefaultResponseErrorHandler

- java.lang.Object
- - org.springframework.web.client.DefaultResponseErrorHandler

- All Implemented Interfaces:
    ResponseErrorHandler
  Direct Known Subclasses:
    ExtractingResponseErrorHandler

      public class DefaultResponseErrorHandler
      extends Object
      implements ResponseErrorHandler

  Spring's default implementation of the `ResponseErrorHandler` interface.

  This error handler checks for the status code on the `ClientHttpResponse`. Any code in the 4xx or 5xx series is considered to be an error. This behavior can be changed by overriding `hasError(HttpStatus)`. Unknown status codes will be ignored by `hasError(ClientHttpResponse)`.

  See `handleError(ClientHttpResponse)` for more details on specific exception types.

  Since:
    3.0
  Author:
    Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
  See Also:
    `RestTemplate.setErrorHandler(org.springframework.web.client.ResponseErrorHandler)`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultResponseErrorHandler()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected Charset` | `getCharset(ClientHttpResponse response)` Determine the charset of the response (for inclusion in a status exception).
    `protected HttpStatus` | `getHttpStatusCode(ClientHttpResponse response)` Deprecated. as of 5.0, in favor of `handleError(ClientHttpResponse, HttpStatus)`
    `protected byte[]` | `getResponseBody(ClientHttpResponse response)` Read the body of the given response (for inclusion in a status exception).
    `void` | `handleError(ClientHttpResponse response)` Handle the error in the given response with the given resolved status code.
    `protected void` | `handleError(ClientHttpResponse response, HttpStatus statusCode)` Handle the error based on the resolved status code.
    `boolean` | `hasError(ClientHttpResponse response)` Delegates to `hasError(HttpStatus)` (for a standard status enum value) or `hasError(int)` (for an unknown status code) with the response status code.
    `protected boolean` | `hasError(HttpStatus statusCode)` Template method called from `hasError(ClientHttpResponse)`.
    `protected boolean` | `hasError(int unknownStatusCode)` Template method called from `hasError(ClientHttpResponse)`.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface org.springframework.web.client.ResponseErrorHandler

      `handleError`

- - ### Constructor Detail

    - #### DefaultResponseErrorHandler

          public DefaultResponseErrorHandler()

  - ### Method Detail

    - #### hasError

          public boolean hasError(ClientHttpResponse response)
                           throws IOException

      Delegates to `hasError(HttpStatus)` (for a standard status enum value) or `hasError(int)` (for an unknown status code) with the response status code.
      Specified by:
        `hasError` in interface `ResponseErrorHandler`
      Parameters:
        `response` - the response to inspect
      Returns:
        `true` if the response indicates an error; `false` otherwise
      Throws:
        `IOException` - in case of I/O errors
      See Also:
        `ClientHttpResponse.getRawStatusCode()`, `hasError(HttpStatus)`, `hasError(int)`

    - #### hasError

          protected boolean hasError(HttpStatus statusCode)

      Template method called from `hasError(ClientHttpResponse)`.

      The default implementation checks `HttpStatus.isError()`. Can be overridden in subclasses.

      Parameters:
        `statusCode` - the HTTP status code as enum value
      Returns:
        `true` if the response indicates an error; `false` otherwise
      See Also:
        `HttpStatus.isError()`

    - #### hasError

          protected boolean hasError(int unknownStatusCode)

      Template method called from `hasError(ClientHttpResponse)`.

      The default implementation checks if the given status code is `CLIENT_ERROR` or `SERVER_ERROR`. Can be overridden in subclasses.

      Parameters:
        `unknownStatusCode` - the HTTP status code as raw value
      Returns:
        `true` if the response indicates an error; `false` otherwise
      Since:
        4.3.21
      See Also:
        `HttpStatus.Series.CLIENT_ERROR`, `HttpStatus.Series.SERVER_ERROR`

    - #### handleError

          public void handleError(ClientHttpResponse response)
                           throws IOException

      Handle the error in the given response with the given resolved status code.

      The default implementation throws:

      - `HttpClientErrorException` if the status code is in the 4xx series, or one of its sub-classes such as `HttpClientErrorException.BadRequest` and others.
      - `HttpServerErrorException` if the status code is in the 5xx series, or one of its sub-classes such as `HttpServerErrorException.InternalServerError` and others.
      - `UnknownHttpStatusCodeException` for error status codes not in the `HttpStatus` enum range.
      Specified by:
        `handleError` in interface `ResponseErrorHandler`
      Parameters:
        `response` - the response with the error
      Throws:
        `UnknownHttpStatusCodeException` - in case of an unresolvable status code
        `IOException` - in case of I/O errors
      See Also:
        `handleError(ClientHttpResponse, HttpStatus)`

    - #### handleError

          protected void handleError(ClientHttpResponse response,
                                     HttpStatus statusCode)
                              throws IOException

      Handle the error based on the resolved status code.

      The default implementation delegates to `HttpClientErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)` for errors in the 4xx range, to `HttpServerErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)` for errors in the 5xx range, or otherwise raises `UnknownHttpStatusCodeException`.

      Throws:
        `IOException`
      Since:
        5.0
      See Also:
        `HttpClientErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)`, `HttpServerErrorException.create(org.springframework.http.HttpStatus, java.lang.String, org.springframework.http.HttpHeaders, byte[], java.nio.charset.Charset)`

    - #### getHttpStatusCode

          @Deprecated
          protected HttpStatus getHttpStatusCode(ClientHttpResponse response)
                                                      throws IOException

      Deprecated. as of 5.0, in favor of `handleError(ClientHttpResponse, HttpStatus)`
      Determine the HTTP status of the given response.
      Parameters:
        `response` - the response to inspect
      Returns:
        the associated HTTP status
      Throws:
        `IOException` - in case of I/O errors
        `UnknownHttpStatusCodeException` - in case of an unknown status code that cannot be represented with the `HttpStatus` enum
      Since:
        4.3.8

    - #### getResponseBody

          protected byte[] getResponseBody(ClientHttpResponse response)

      Read the body of the given response (for inclusion in a status exception).
      Parameters:
        `response` - the response to inspect
      Returns:
        the response body as a byte array, or an empty byte array if the body could not be read
      Since:
        4.3.8

    - #### getCharset

          @Nullable
          protected Charset getCharset(ClientHttpResponse response)

      Determine the charset of the response (for inclusion in a status exception).
      Parameters:
        `response` - the response to inspect
      Returns:
        the associated charset, or `null` if none
      Since:
        4.3.8