@Override // Ignore 400 public void handleError(ClientHttpResponse response) throws IOException { if (response.getRawStatusCode() != 400) { super.handleError(response); } } });
/** * Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or * {@link #hasError(int)} (for an unknown status code) with the response status code. * @see ClientHttpResponse#getRawStatusCode() * @see #hasError(HttpStatus) * @see #hasError(int) */ @Override public boolean hasError(ClientHttpResponse response) throws IOException { int rawStatusCode = response.getRawStatusCode(); HttpStatus statusCode = HttpStatus.resolve(rawStatusCode); return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode)); }
/** * Construct an error handler that can deal with OAuth2 concerns before handling the error in the default fashion. */ public OAuth2ErrorHandler(OAuth2ProtectedResourceDetails resource) { this.resource = resource; this.errorHandler = new DefaultResponseErrorHandler(); }
/** * Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the * response status code. * @throws UnknownHttpStatusCodeException in case of an unresolvable status code * @see #handleError(ClientHttpResponse, HttpStatus) */ @Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode()); if (statusCode == null) { throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } handleError(response, statusCode); }
/** * Determine the HTTP status of the given response. * @param response the response to inspect * @return the associated HTTP status * @throws IOException in case of I/O errors * @throws UnknownHttpStatusCodeException in case of an unknown status code * that cannot be represented with the {@link HttpStatus} enum * @since 4.3.8 * @deprecated as of 5.0, in favor of {@link #handleError(ClientHttpResponse, HttpStatus)} */ @Deprecated protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException { HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode()); if (statusCode == null) { throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } return statusCode; }
/** * <p>The default implementation throws a {@link HttpClientErrorException} if the response status code is * {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException} * if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}, * and a {@link RestClientException} in other cases. */ public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = response.getStatusCode(); MediaType contentType = response.getHeaders().getContentType(); Charset charset = contentType != null ? contentType.getCharSet() : null; byte[] body = getResponseBody(response); switch (statusCode.series()) { case CLIENT_ERROR: throw new HttpClientErrorException(statusCode, response.getStatusText(), body, charset); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText(), body, charset); default: throw new RestClientException("Unknown status code [" + statusCode + "]"); } }
/** * Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the * response status code. * @throws UnknownHttpStatusCodeException in case of an unresolvable status code * @see #handleError(ClientHttpResponse, HttpStatus) */ @Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode()); if (statusCode == null) { throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } handleError(response, statusCode); }
/** * Handle the error in the given response with the given resolved status code. * <p>The default implementation throws an {@link HttpClientErrorException} * if the status code is {@link HttpStatus.Series#CLIENT_ERROR}, an * {@link HttpServerErrorException} if it is {@link HttpStatus.Series#SERVER_ERROR}, * and an {@link UnknownHttpStatusCodeException} in other cases. * @since 5.0 * @see HttpClientErrorException#create * @see HttpServerErrorException#create */ protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { String statusText = response.getStatusText(); HttpHeaders headers = response.getHeaders(); byte[] body = getResponseBody(response); Charset charset = getCharset(response); switch (statusCode.series()) { case CLIENT_ERROR: throw HttpClientErrorException.create(statusCode, statusText, headers, body, charset); case SERVER_ERROR: throw HttpServerErrorException.create(statusCode, statusText, headers, body, charset); default: throw new UnknownHttpStatusCodeException(statusCode.value(), statusText, headers, body, charset); } }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (this.statusMapping.containsKey(statusCode)) { extract(this.statusMapping.get(statusCode), response); } else if (this.seriesMapping.containsKey(statusCode.series())) { extract(this.seriesMapping.get(statusCode.series()), response); } else { super.handleError(response, statusCode); } }
/** * Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the * response status code. * @throws UnknownHttpStatusCodeException in case of an unresolvable status code * @see #handleError(ClientHttpResponse, HttpStatus) */ @Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode()); if (statusCode == null) { throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } handleError(response, statusCode); }
@Override protected boolean hasError(HttpStatus statusCode) { if (this.statusMapping.containsKey(statusCode)) { return this.statusMapping.get(statusCode) != null; } else if (this.seriesMapping.containsKey(statusCode.series())) { return this.seriesMapping.get(statusCode.series()) != null; } else { return super.hasError(statusCode); } }
@Test public void httpCustomStatus() throws MalformedURLException { expectedEx.expectMessage(equalTo("Server Error")); expectedEx.expect(JsonRpcClientException.class); RestTemplate restTemplate = new RestTemplate(); JsonRpcRestClient client = getClient(JettyServer.SERVLET, restTemplate); // Overwrite error handler for error check. restTemplate.setErrorHandler(new DefaultResponseErrorHandler()); FakeServiceInterface service = ProxyUtil.createClientProxy(FakeServiceInterface.class, client); service.throwSomeException("function error"); }
/** * Determine the HTTP status of the given response. * @param response the response to inspect * @return the associated HTTP status * @throws IOException in case of I/O errors * @throws UnknownHttpStatusCodeException in case of an unknown status code * that cannot be represented with the {@link HttpStatus} enum * @since 4.3.8 * @deprecated as of 5.0, in favor of {@link #handleError(ClientHttpResponse, HttpStatus)} */ @Deprecated protected HttpStatus getHttpStatusCode(ClientHttpResponse response) throws IOException { HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode()); if (statusCode == null) { throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } return statusCode; }
@Override // Ignore 400 public void handleError(ClientHttpResponse response) throws IOException { if (response.getRawStatusCode() != 400) { super.handleError(response); } } });
/** * Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the * response status code. * @throws UnknownHttpStatusCodeException in case of an unresolvable status code * @see #handleError(ClientHttpResponse, HttpStatus) */ @Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode()); if (statusCode == null) { throw new UnknownHttpStatusCodeException(response.getRawStatusCode(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } handleError(response, statusCode); }
/** * Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or * {@link #hasError(int)} (for an unknown status code) with the response status code. * @see ClientHttpResponse#getRawStatusCode() * @see #hasError(HttpStatus) * @see #hasError(int) */ @Override public boolean hasError(ClientHttpResponse response) throws IOException { int rawStatusCode = response.getRawStatusCode(); HttpStatus statusCode = HttpStatus.resolve(rawStatusCode); return (statusCode != null ? hasError(statusCode) : hasError(rawStatusCode)); }
/** * Construct an error handler that can deal with OAuth2 concerns before handling the error in the default fashion. */ public OAuth2ErrorHandler(OAuth2ProtectedResourceDetails resource) { this.resource = resource; this.errorHandler = new DefaultResponseErrorHandler(); }
/** * Handle the error in the given response with the given resolved status code. * <p>The default implementation throws an {@link HttpClientErrorException} * if the status code is {@link HttpStatus.Series#CLIENT_ERROR}, an * {@link HttpServerErrorException} if it is {@link HttpStatus.Series#SERVER_ERROR}, * and an {@link UnknownHttpStatusCodeException} in other cases. * @since 5.0 * @see HttpClientErrorException#create * @see HttpServerErrorException#create */ protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { String statusText = response.getStatusText(); HttpHeaders headers = response.getHeaders(); byte[] body = getResponseBody(response); Charset charset = getCharset(response); switch (statusCode.series()) { case CLIENT_ERROR: throw HttpClientErrorException.create(statusCode, statusText, headers, body, charset); case SERVER_ERROR: throw HttpServerErrorException.create(statusCode, statusText, headers, body, charset); default: throw new UnknownHttpStatusCodeException(statusCode.value(), statusText, headers, body, charset); } }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (this.statusMapping.containsKey(statusCode)) { extract(this.statusMapping.get(statusCode), response); } else if (this.seriesMapping.containsKey(statusCode.series())) { extract(this.seriesMapping.get(statusCode.series()), response); } else { super.handleError(response, statusCode); } }
@Override protected boolean hasError(HttpStatus statusCode) { if (this.statusMapping.containsKey(statusCode)) { return this.statusMapping.get(statusCode) != null; } else if (this.seriesMapping.containsKey(statusCode.series())) { return this.seriesMapping.get(statusCode.series()) != null; } else { return super.hasError(statusCode); } }