@ExceptionHandler(HttpStatusCodeException.class) public ResponseEntity<Map<String, Object>> restTemplateException(HttpServletRequest request, HttpStatusCodeException ex) { return handleError(request, ex.getStatusCode(), ex); }
private ResponseEntity<Map<String, Object>> handleError(HttpServletRequest request, HttpStatus status, Throwable ex, Level logLevel) { String message = ex.getMessage(); printLog(message, ex, logLevel); Map<String, Object> errorAttributes = new HashMap<>(); boolean errorHandled = false; if (ex instanceof HttpStatusCodeException) { try { //try to extract the original error info if it is thrown from apollo programs, e.g. admin service errorAttributes = gson.fromJson(((HttpStatusCodeException) ex).getResponseBodyAsString(), mapType); status = ((HttpStatusCodeException) ex).getStatusCode(); errorHandled = true; } catch (Throwable th) { //ignore } } if (!errorHandled) { errorAttributes.put("status", status.value()); errorAttributes.put("message", message); errorAttributes.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); errorAttributes.put("exception", ex.getClass().getName()); } HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON_UTF8); return new ResponseEntity<>(errorAttributes, headers, status); }
if (!e.getStatusCode().equals(HttpStatus.NOT_FOUND)) { LOG.error("Unable to watch consul path " + this.path, e); return null;
/** * @param headers must not be {@literal null}. * @param backend secret backend mount path, must not be {@literal null}. * @param key key within the key-value secret backend, must not be {@literal null}. * @return */ @Override public String getData(HttpHeaders headers, String backend, String key) { try { String urlTemplate = String.format("%s/v1/%s/%s", baseUrl, backend, getPath()); ResponseEntity<VaultResponse> response = rest.exchange(urlTemplate, HttpMethod.GET, new HttpEntity<>(headers), VaultResponse.class, key); HttpStatus status = response.getStatusCode(); if (status == HttpStatus.OK) { return extractDataFromBody(response.getBody()); } } catch (HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.NOT_FOUND) { return null; } throw e; } return null; } }
if (ex instanceof HttpStatusCodeException) { if (((HttpStatusCodeException) ex).getStatusCode().equals(HttpStatus.NOT_FOUND) && createIfNotExists) { createItem(appId, env, clusterName, namespaceName, item, request); return;
result = "Get FAILED with HttpStatusCode: " + e.getStatusCode() + "|" + e.getStatusText(); } catch (RuntimeException e) { result = "Get FAILED\n" + ExceptionUtils.getFullStackTrace(e);
} catch (HttpStatusCodeException httpStatusCodeException) { logger.error("HTTP Error code={} status={}\nresponse={}" , httpStatusCodeException.getStatusCode().value() , httpStatusCodeException.getStatusText() , httpStatusCodeException.getResponseBodyAsString() ); Integer jsonErrorCode = DefaultHttpStatusCodeProvider.INSTANCE.getJsonRpcCode(httpStatusCodeException.getStatusCode().value()); if (jsonErrorCode == null) { jsonErrorCode = httpStatusCodeException.getStatusCode().value();
throw new OperationFailureException(operr("failed to %s to %s, status code: %s, response body: %s", method.toString().toLowerCase(), url, e.getStatusCode(), e.getResponseBodyAsString())); } catch (ResourceAccessException e) { throw new OperationFailureException(operr("failed to %s to %s, IO Error: %s", method.toString().toLowerCase(), url, e.getMessage()));
try { restTemplate.postForObject(url, pojoInstance, responseClass); } catch (HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) { String responseString = e.getResponseBodyAsString(); ObjectMapper mapper = new ObjectMapper(); CustomError result = mapper.readValue(responseString, CustomError.class); } }
@ExceptionHandler @ResponseBody public ResponseEntity<Messages> handleHttpStatusCodeException(HttpStatusCodeException e) { return handleInternal(e, null, e.getStatusCode()); }
protected void processStatusCodeException(HttpStatusCodeException e) { String message; try { message = MessageFormat.format("Response from server: {0} {1}", e.getStatusCode().value(), e.getStatusText()); //we do not read error body, because it contains html code in some cases } catch (Exception ex) { message = e.getStatusText(); } log.error("Error from server: {}", message, e); }
private String createHttpErrorResponseString(HttpStatusCodeException ex) { String responseBody = ex.getResponseBodyAsString().trim(); if (responseBody.startsWith("null")) { return ex.getStatusCode().getReasonPhrase(); } return responseBody; }
private String createHttpErrorResponseString(HttpStatusCodeException ex) { String responseBody = ex.getResponseBodyAsString().trim(); if (responseBody.startsWith("null")) { return ex.getStatusCode().getReasonPhrase(); } return responseBody; }
@Override public void delete(String path) { Assert.hasText(path, "Path must not be empty"); try { sessionTemplate.delete(path); } catch (HttpStatusCodeException e) { if (e.getStatusCode() == HttpStatus.NOT_FOUND) { return; } throw VaultResponses.buildException(e, path); } }
@Override public int status() { return ex.getStatusCode().value(); }
private String formatHttpException(HttpStatusCodeException e) { try { return MessageFormat.format("Response from server: {0} {1}\n {2}", e.getStatusCode().value(), e.getStatusText(),// getResponseBodyAsString - below org.springframework.util.StringUtils.trimWhitespace(e.getResponseBodyAsString())); } catch (Exception ex) { log.error("Can not format exception {}", e, ex); } return e.getStatusText(); }
public void sendStatisticalData(SimpleValidationReport simpleValidationReport) { if (!properties.isEnabled()) { return; } try { HttpEntity<String> entity = new HttpEntity<>(composeBatchRequestBody(simpleValidationReport)); restTemplate.postForObject(properties.getUrl(), entity, byte[].class); } catch (UnsupportedEncodingException e) { LOGGER.error("Error on URI-encoding request body: {}", e.getMessage(), e); } catch (HttpStatusCodeException e) { LOGGER.error("Batch request failed with status code: {}", e.getStatusCode(), e); } }
private HermesResponse fromHttpStatusCodeException(HttpStatusCodeException exception) { return hermesResponse() .withHttpStatus(exception.getStatusCode().value()) .withBody(exception.getResponseBodyAsString()) .build(); } }
private static HttpStatusCodeException prepareHttpStatusCodeException(HttpStatus statusCode, String statusText, String locationOfFileContainingResponseBody) throws IOException { HttpStatusCodeException exception = Mockito.mock(HttpStatusCodeException.class); Mockito.when(exception.getStatusCode()) .thenReturn(statusCode); Mockito.when(exception.getStatusText()) .thenReturn(statusText); String responseBody = TestUtil.getResourceAsString(locationOfFileContainingResponseBody, CustomControllerClientErrorHandlerTest.class); Mockito.when(exception.getResponseBodyAsString()) .thenReturn(responseBody); return exception; }
private CloudOperationException asCloudOperationException(HttpStatusCodeException exception) { String description = getDescriptionFromResponseBody(exception.getResponseBodyAsString()); return new CloudOperationException(exception.getStatusCode(), exception.getStatusText(), description); }