@Override public void executeSendRequest(URI url, HttpHeaders headers, TextMessage message) { if (logger.isTraceEnabled()) { logger.trace("Starting XHR send, url=" + url); } ResponseEntity<String> response = executeSendRequestInternal(url, headers, message); if (response.getStatusCode() != HttpStatus.NO_CONTENT) { if (logger.isErrorEnabled()) { logger.error("XHR send request (url=" + url + ") failed: " + response); } throw new HttpServerErrorException(response.getStatusCode()); } if (logger.isTraceEnabled()) { logger.trace("XHR send request (url=" + url + ") response: " + response); } }
@Override public String executeInfoRequest(URI infoUrl, @Nullable HttpHeaders headers) { if (logger.isDebugEnabled()) { logger.debug("Executing SockJS Info request, url=" + infoUrl); } HttpHeaders infoRequestHeaders = new HttpHeaders(); if (headers != null) { infoRequestHeaders.putAll(headers); } ResponseEntity<String> response = executeInfoRequestInternal(infoUrl, infoRequestHeaders); if (response.getStatusCode() != HttpStatus.OK) { if (logger.isErrorEnabled()) { logger.error("SockJS Info request (url=" + infoUrl + ") failed: " + response); } throw new HttpServerErrorException(response.getStatusCode()); } if (logger.isTraceEnabled()) { logger.trace("SockJS Info request (url=" + infoUrl + ") response: " + response); } String result = response.getBody(); return (result != null ? result : ""); }
@Override public void onBegin(Response response) { if (response.getStatus() != 200) { HttpStatus status = HttpStatus.valueOf(response.getStatus()); response.abort(new HttpServerErrorException(status, "Unexpected XHR receive status")); } }
/** * Create an {@code HttpServerErrorException} or an HTTP status specific sub-class. * @since 5.1 */ public static HttpServerErrorException create( HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) { switch (statusCode) { case INTERNAL_SERVER_ERROR: return new HttpServerErrorException.InternalServerError(statusText, headers, body, charset); case NOT_IMPLEMENTED: return new HttpServerErrorException.NotImplemented(statusText, headers, body, charset); case BAD_GATEWAY: return new HttpServerErrorException.BadGateway(statusText, headers, body, charset); case SERVICE_UNAVAILABLE: return new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset); case GATEWAY_TIMEOUT: return new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset); default: return new HttpServerErrorException(statusCode, statusText, headers, body, charset); } }
throw new HttpServerErrorException( httpStatus, response.getStatusText(), response.getHeaders(), null, null);
private static <T> Publisher<? extends T> handleServerError(Request request, ClientResponse response) { return Mono.error( new HttpServerErrorException(response.statusCode(), String.format("%s request to %s returned error code %s.", request.getMethod(), request.getEndpoint(), response.statusCode().value()))); }
/** * Create an {@code HttpServerErrorException} or an HTTP status specific sub-class. * @since 5.1 */ public static HttpServerErrorException create( HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) { switch (statusCode) { case INTERNAL_SERVER_ERROR: return new HttpServerErrorException.InternalServerError(statusText, headers, body, charset); case NOT_IMPLEMENTED: return new HttpServerErrorException.NotImplemented(statusText, headers, body, charset); case BAD_GATEWAY: return new HttpServerErrorException.BadGateway(statusText, headers, body, charset); case SERVICE_UNAVAILABLE: return new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset); case GATEWAY_TIMEOUT: return new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset); default: return new HttpServerErrorException(statusCode, statusText, headers, body, charset); } }
@Test public void errorHandling() throws Exception { String url = "http://example.com"; mockSentRequest(GET, url); mockResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR); willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)) .given(errorHandler).handleError(new URI(url), GET, response); try { template.execute(url, GET, null, null); fail("HttpServerErrorException expected"); } catch (HttpServerErrorException ex) { // expected } verify(response).close(); }
@Test public void connectFailure() throws Exception { final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR); RestOperations restTemplate = mock(RestOperations.class); given(restTemplate.execute((URI) any(), eq(HttpMethod.POST), any(), any())).willThrow(expected); final CountDownLatch latch = new CountDownLatch(1); connect(restTemplate).addCallback( new ListenableFutureCallback<WebSocketSession>() { @Override public void onSuccess(WebSocketSession result) { } @Override public void onFailure(Throwable ex) { if (ex == expected) { latch.countDown(); } } } ); verifyNoMoreInteractions(this.webSocketHandler); }
@Test public void connectInfoRequestFailure() throws URISyntaxException { HttpServerErrorException exception = new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE); given(this.infoReceiver.executeInfoRequest(any(), any())).willThrow(exception); this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback); verify(this.connectCallback).onFailure(exception); assertFalse(this.webSocketTransport.invoked()); assertFalse(this.xhrTransport.invoked()); }
@Override public void handleError(ClientHttpResponse response) throws IOException { final HttpStatus statusCode = getHttpStatusCode(response); switch (statusCode.series()) { case CLIENT_ERROR: throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); default: throw new RestClientException("Unknown status code [" + statusCode + "]"); } }
@Override public void executeSendRequest(URI url, HttpHeaders headers, TextMessage message) { if (logger.isTraceEnabled()) { logger.trace("Starting XHR send, url=" + url); } ResponseEntity<String> response = executeSendRequestInternal(url, headers, message); if (response.getStatusCode() != HttpStatus.NO_CONTENT) { if (logger.isErrorEnabled()) { logger.error("XHR send request (url=" + url + ") failed: " + response); } throw new HttpServerErrorException(response.getStatusCode()); } if (logger.isTraceEnabled()) { logger.trace("XHR send request (url=" + url + ") response: " + response); } }
@Override public void executeSendRequest(URI url, HttpHeaders headers, TextMessage message) { if (logger.isTraceEnabled()) { logger.trace("Starting XHR send, url=" + url); } ResponseEntity<String> response = executeSendRequestInternal(url, headers, message); if (response.getStatusCode() != HttpStatus.NO_CONTENT) { if (logger.isErrorEnabled()) { logger.error("XHR send request (url=" + url + ") failed: " + response); } throw new HttpServerErrorException(response.getStatusCode()); } if (logger.isTraceEnabled()) { logger.trace("XHR send request (url=" + url + ") response: " + response); } }
@Override public void onBegin(Response response) { if (response.getStatus() != 200) { HttpStatus status = HttpStatus.valueOf(response.getStatus()); response.abort(new HttpServerErrorException(status, "Unexpected XHR receive status")); } }
@Override public void onBegin(Response response) { if (response.getStatus() != 200) { HttpStatus status = HttpStatus.valueOf(response.getStatus()); response.abort(new HttpServerErrorException(status, "Unexpected XHR receive status")); } }
protected void handleServerErro(HttpStatus statusCode, ClientHttpResponse response) throws IOException{ throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); }
@Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = response.getStatusCode(); switch (statusCode.series()) { case CLIENT_ERROR: throw getException(response); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText()); default: throw new RestClientException("Unknown status code [" + 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 + "]"); } }
/** * This 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. */ @Override public void handleError(ClientHttpResponse response) throws IOException { HttpStatus statusCode = getHttpStatusCode(response); switch (statusCode.series()) { case CLIENT_ERROR: throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); case SERVER_ERROR: throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); default: throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } }
@Override public void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException { if (statusCode.series() == HttpStatus.Series.CLIENT_ERROR) { throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } else if (statusCode.series() == HttpStatus.Series.SERVER_ERROR) { throw new HttpServerErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } else { try { HttpStatus.Series.valueOf(statusCode); super.handleError(response, statusCode); } catch (Exception e) { throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response)); } } }