Example usage for org.springframework.web.client HttpClientErrorException HttpClientErrorException

List of usage examples for org.springframework.web.client HttpClientErrorException HttpClientErrorException

Introduction

In this page you can find the example usage for org.springframework.web.client HttpClientErrorException HttpClientErrorException.

Prototype

public HttpClientErrorException(HttpStatus statusCode, String statusText, @Nullable HttpHeaders headers,
        @Nullable byte[] body, @Nullable Charset responseCharset) 

Source Link

Document

Constructor with a status code and status text, headers, and content.

Usage

From source file:com.sastix.cms.common.client.exception.CommonExceptionHandler.java

/**
 * This default implementation throws a {@link HttpClientErrorException} if the response status code
 * is {@link HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
 * if it is {@link HttpStatus.Series#SERVER_ERROR},
 * and a {@link RestClientException} in other cases.
 *//*from   w  ww. j a va  2 s .  c o  m*/
@Override
public void handleError(ClientHttpResponse response) throws IOException, RestClientException {
    HttpStatus statusCode = getHttpStatusCode(response);
    switch (statusCode.series()) {
    case CLIENT_ERROR:
        final byte[] responseBody = getResponseBody(response);
        final Charset charset = getCharset(response);
        final String statusText = response.getStatusText();
        final HttpHeaders httpHeaders = response.getHeaders();
        final RestErrorDTO errorDTO;

        try {
            errorDTO = objectMapper.readValue(new String(responseBody, charset), RestErrorDTO.class);
            LOG.error("Exception: " + errorDTO.toString());
        } catch (final Exception e) {
            //Wasn't able to map String on ErrorDTO.
            //It is an Unknown Exception
            //Throw Default Exception
            final HttpClientErrorException clientErrorException = new HttpClientErrorException(statusCode,
                    statusText, httpHeaders, responseBody, charset);
            LOG.error("Unknown Exception: " + clientErrorException.getMessage());
            throw clientErrorException;
        }

        if (exceptionClasses.containsKey(errorDTO.getException())) {
            throw (exceptionClasses.get(errorDTO.getException())).create(errorDTO.getMessage());
        } else {
            throw new HttpClientErrorException(statusCode, statusText, httpHeaders, responseBody, charset);
        }

    case SERVER_ERROR:
        throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(),
                getResponseBody(response), getCharset(response));
    default:
        throw new RestClientException("Unknown status code [" + statusCode + "]");
    }
}