public class DefaultResponseErrorHandler extends Object implements ResponseErrorHandler
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.
RestTemplate.setErrorHandler(org.springframework.web.client.ResponseErrorHandler)
Constructor and Description |
---|
DefaultResponseErrorHandler() |
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) . |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handleError
public boolean hasError(ClientHttpResponse response) throws IOException
hasError(HttpStatus)
(for a standard status enum value) or
hasError(int)
(for an unknown status code) with the response status code.hasError
in interface ResponseErrorHandler
response
- the response to inspecttrue
if the response indicates an error; false
otherwiseIOException
- in case of I/O errorsClientHttpResponse.getRawStatusCode()
,
hasError(HttpStatus)
,
hasError(int)
protected boolean hasError(HttpStatus statusCode)
hasError(ClientHttpResponse)
.
The default implementation checks HttpStatus.isError()
.
Can be overridden in subclasses.
statusCode
- the HTTP status code as enum valuetrue
if the response indicates an error; false
otherwiseHttpStatus.isError()
protected boolean hasError(int unknownStatusCode)
hasError(ClientHttpResponse)
.
The default implementation checks if the given status code is
CLIENT_ERROR
or
SERVER_ERROR
.
Can be overridden in subclasses.
unknownStatusCode
- the HTTP status code as raw valuetrue
if the response indicates an error; false
otherwiseHttpStatus.Series.CLIENT_ERROR
,
HttpStatus.Series.SERVER_ERROR
public void handleError(ClientHttpResponse response) throws IOException
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.
handleError
in interface ResponseErrorHandler
response
- the response with the errorUnknownHttpStatusCodeException
- in case of an unresolvable status codeIOException
- in case of I/O errorshandleError(ClientHttpResponse, HttpStatus)
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException
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
.
IOException
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)
@Deprecated protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException
handleError(ClientHttpResponse, HttpStatus)
response
- the response to inspectIOException
- in case of I/O errorsUnknownHttpStatusCodeException
- in case of an unknown status code
that cannot be represented with the HttpStatus
enumprotected byte[] getResponseBody(ClientHttpResponse response)
response
- the response to inspect@Nullable protected Charset getCharset(ClientHttpResponse response)
response
- the response to inspectnull
if none