ex.getStatusText()); assertEquals(MediaType.TEXT_PLAIN, ex.getHeaders().getContentType()); assertEquals(errorMessage, ex.getResponseBodyAsString());
private static String format(String message, WebClientResponseException e) { return String.format("%s: Status %s %s %s", message, e.getRawStatusCode(), e.getStatusText(), VaultResponses.getError(e.getResponseBodyAsString())); }
@ExceptionHandler @ResponseBody public ResponseEntity<Object> handleStatusCodeException(WebClientResponseException e) { // Map the status to a 500 unlesss it's a 401. If that's the case // then we know the client has passed in an invalid X-xxx-Authorization header and it's a 400. logger.error("Backend returned {} {} \n {}", e.getStatusCode(), e.getStatusCode().getReasonPhrase(), e.getResponseBodyAsString()); String backendStatus = Integer.toString(e.getRawStatusCode()); if (e.getStatusCode() == UNAUTHORIZED) { Map<String, String> body = Collections.singletonMap("error", "invalid_connector_token"); return ResponseEntity.status(BAD_REQUEST) .header(BACKEND_STATUS, backendStatus) .contentType(APPLICATION_JSON) .body(body); } else { BodyBuilder builder = ResponseEntity.status(INTERNAL_SERVER_ERROR) .header(BACKEND_STATUS, backendStatus); if (e.getHeaders().getContentType() != null) { builder.contentType(e.getHeaders().getContentType()); } return builder.body(e.getResponseBodyAsString()); } } }