@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 onFailure(Throwable ex) { assertTrue(ex instanceof HttpServerErrorException); HttpServerErrorException hsex = (HttpServerErrorException) ex; assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, hsex.getStatusCode()); assertNotNull(hsex.getStatusText()); assertNotNull(hsex.getResponseBodyAsString()); } });
error = e; if (MediaType.APPLICATION_JSON .includes(e.getResponseHeaders().getContentType())) { errorBody = e.getResponseBodyAsString();
throw new HttpServerErrorException(response.getStatusCode()); throw new LinkedDataFetchingException(resourceURI, MessageFormat.format( "caught a HttpServerErrorException exception, for {0}. Underlying error message is: {1}, response Body: {2}", resourceURI, e.getMessage(), ((HttpServerErrorException) e).getResponseBodyAsString()), e);
private Map<String, Object> postForMap(String path, MultiValueMap<String, String> formData, HttpHeaders headers) { if (headers.getContentType() == null) { headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); } @SuppressWarnings("rawtypes") Map map = new HashMap(); try { map = restTemplate.exchange(path, HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(formData, headers), Map.class).getBody(); } catch (HttpClientErrorException e1) { logger.error("catch token exception when check token!", e1); map.put(ERROR, e1.getStatusCode()); } catch (HttpServerErrorException e2) { logger.error("catch no permission exception when check token!", e2); map.put(ERROR, e2.getStatusCode()); } catch (Exception e) { logger.error("catch common exception when check token!", e); } @SuppressWarnings("unchecked") Map<String, Object> result = map; return result; }
@Test public void shouldReturn500WhenSendingRequestWithAnyMethodToTheBookingIsDownService() { URI uri = UriComponentsBuilder.fromHttpUrl("http://www.booking-is-down.com") .path("/api/bookings/12345") .build() .toUri(); Throwable throwable = catchThrowable(() -> restTemplate.exchange(uri, HttpMethod.DELETE, null, Void.class)); assertThat(throwable).isInstanceOf(HttpServerErrorException.class); HttpServerErrorException exception = (HttpServerErrorException) throwable; // Then assertThat(exception.getStatusCode()).isEqualTo(INTERNAL_SERVER_ERROR); assertThat(exception.getResponseBodyAsString()).isEqualTo("booking is down"); }
@Override public Response toResponse(HttpServerErrorException exception) { RestError error = new RestError( exception.getMessage(), exception.getMessage(), ErrorMap.from(new String(exception.getResponseBodyAsByteArray(), StandardCharsets.UTF_8)), exception.getStatusCode().value()); return Response.status(exception.getStatusCode().value()).type(MediaType.APPLICATION_JSON_TYPE).entity(error).build(); } }
String responseBody = ex.getResponseBodyAsString(); log.error( "Internal error happened during event data push: " + responseBody, ex );
} catch (HttpServerErrorException ex) { HttpHeaders headers = ex.getResponseHeaders(); String failureText = StringUtils.trimToNull(headers.getFirst("failure")); String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));
/** * 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); } }
private OutboundMessageResponse send( UriComponentsBuilder uriBuilder ) { ResponseEntity<String> responseEntity = null; try { URI url = uriBuilder.build().encode().toUri(); responseEntity = restTemplate.exchange( url, HttpMethod.POST, null, String.class ); } catch ( HttpClientErrorException ex ) { log.error( "Client error " + ex.getMessage() ); } catch ( HttpServerErrorException ex ) { log.error( "Server error " + ex.getMessage() ); } catch ( Exception ex ) { log.error( "Error " + ex.getMessage() ); } return getResponse( responseEntity ); }
public HttpStatus send( String urlTemplate, HttpEntity<?> request, Class<?> klass ) { ResponseEntity<?> response; HttpStatus statusCode = null; try { response = restTemplate.exchange( urlTemplate, HttpMethod.POST, request, klass ); statusCode = response.getStatusCode(); } catch ( HttpClientErrorException ex ) { log.error( "Client error", ex ); statusCode = ex.getStatusCode(); } catch ( HttpServerErrorException ex ) { log.error( "Server error", ex ); statusCode = ex.getStatusCode(); } catch ( Exception ex ) { log.error( "Error", ex ); statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } log.info( "Response status code: " + statusCode ); return statusCode; }
@Test public void shouldFailToQueryBookingsIfUnexpectedQueryParamIsPresent() { // When URI uri = UriComponentsBuilder.fromHttpUrl("http://www.my-test.com") .path("/api/bookings/1") .queryParam("page", 1) .build() .toUri(); Throwable throwable = catchThrowable(() -> restTemplate.getForEntity(uri, SimpleBooking.class)); // Then assertThat(throwable).isInstanceOf(HttpServerErrorException.class); HttpServerErrorException exception = (HttpServerErrorException) throwable; assertThat(exception.getStatusCode()).isEqualTo(BAD_GATEWAY); assertThat(exception.getResponseBodyAsString()).containsIgnoringCase("Hoverfly error"); }
String responseBody = ex.getResponseBodyAsString(); log.error( "Internal error happened during completeness push: " + responseBody, ex ); throw ex;
} catch (HttpServerErrorException ex) { HttpHeaders headers = ex.getResponseHeaders(); String failureText = StringUtils.trimToNull(headers.getFirst("failure")); String errorText = StringUtils.trimToNull(headers.getFirst("XA_XAER"));
/** * 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); } }
/** * HttpServerErrorException. * http状态码为503 * * @param ex HttpServerErrorException * @return ModelAndView */ @ExceptionHandler(HttpServerErrorException.class) @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE) public ModelAndView handleHttpServerException(HttpServerErrorException ex, HttpServletRequest request, HttpServletResponse response) { ErrorCode errorCode = createError(999, ex.getMessage(), HttpStatus.SERVICE_UNAVAILABLE); SystemException systemException = SystemException.wrap(errorCode, ex) .set("details", request.getServletPath()); return handleSystemException(systemException, request, 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 : ""); }
@Test public void serverError() { try { template.execute(baseUrl + "/status/server", HttpMethod.GET, null, null); fail("HttpServerErrorException expected"); } catch (HttpServerErrorException ex) { assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode()); assertNotNull(ex.getStatusText()); assertNotNull(ex.getResponseBodyAsString()); } }
private MavenVersionsSearch getFromMavenCentral(Map<String,String> _params) throws InterruptedException{ final RestTemplate rest_template = new RestTemplate(); ResponseEntity<MavenVersionsSearch> responseEntity = null; for(Integer i=1;i<this.mavenCentralRetryCount+1;i++){ try{ responseEntity = rest_template.getForEntity(baseUrl, MavenVersionsSearch.class, _params); break; } catch(HttpServerErrorException he){ MavenCentralWrapper.log.error("HttpServerErrorException: Received status code ["+he.getStatusCode() + "] calling url ["+baseUrl+"] with app ["+_params.get("q")+"], retry from [" +i+"]"); Thread.sleep(i*mavenCentralDelay); } catch(HttpClientErrorException he){ MavenCentralWrapper.log.warn("HttpClientErrorException: Received status code ["+he.getStatusCode() + "] calling url ["+baseUrl+"] with app ["+_params.get("q")+"] :" + he.getMessage()); } } if(responseEntity!=null) return responseEntity.getBody(); else { log.warn("Maven central was unavailable and could not serve the request ["+baseUrl+"] with params ["+_params.get("q")+"]"); return null; } }