assertTrue(throwable instanceof WebClientResponseException); WebClientResponseException ex = (WebClientResponseException) throwable; assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, ex.getStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getRawStatusCode()); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),
public static <R> Mono<R> skipOnStatus(Throwable throwable, Predicate<HttpStatus> statusPredicate) { if (throwable instanceof WebClientResponseException && statusPredicate.test(WebClientResponseException.class.cast(throwable).getStatusCode())) { return Mono.empty(); } else { return Mono.error(throwable); } }
@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()); } } }
if (e.getStatusCode().is4xxClientError()) {
private Throwable mapError(Throwable throwable) { if (throwable instanceof WebClientResponseException) { HttpStatus statusCode = ((WebClientResponseException) throwable) .getStatusCode(); if (statusCode.equals(HttpStatus.FORBIDDEN)) { return new CloudFoundryAuthorizationException(Reason.ACCESS_DENIED, "Access denied"); } if (statusCode.is4xxClientError()) { return new CloudFoundryAuthorizationException(Reason.INVALID_TOKEN, "Invalid token", throwable); } } return new CloudFoundryAuthorizationException(Reason.SERVICE_UNAVAILABLE, "Cloud controller not reachable"); }