- @ExceptionHandler(HttpStatusCodeException.class)
- public final ResponseEntity<ErrorTransport> handleHttpStatusCodeException(HttpStatusCodeException ex,
- WebRequest request) {
- log.warn("handleHttpStatusCodeException: request {}, status code {}", request.getDescription(false),
- ex.getStatusCode());
- return new ResponseEntity<>(new ErrorTransport(ex.getRawStatusCode(), ex.getResponseBodyAsString(), ex),
- HttpStatus.BAD_GATEWAY);
- }
-
- /*
- * This exception also happens when Spring security denies access to a method
- * due to missing/wrong roles (granted authorities). Override the method to
- * answer permission denied, even though that may obscure a genuine developer
- * error.
- *
- * The web request that arrives here has URI /error; how to obtain the URI of
- * the original request?!?
- */
- @Override
- public final ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
- HttpHeaders headers, HttpStatus status, WebRequest request) {
- log.warn("handleHttpRequestMethodNotSupported: answering 'permission denied' for method {}", ex.getMethod());
- return new ResponseEntity<Object>(new ErrorTransport(HttpStatus.UNAUTHORIZED.value(),
- "Permission denied for method " + ex.getMethod(), ex), HttpStatus.UNAUTHORIZED);
+ @ExceptionHandler({ RestClientResponseException.class })
+ public final ResponseEntity<ErrorTransport> handleProxyMethodException(Exception ex, WebRequest request) {
+ // Capture the full stack trace in the log.
+ log.error("handleProxyMethodException: request {}, exception {}", request.getDescription(false), ex);
+ if (ex instanceof HttpStatusCodeException) {
+ HttpStatusCodeException hsce = (HttpStatusCodeException) ex;
+ return new ResponseEntity<>(new ErrorTransport(hsce.getRawStatusCode(), hsce.getResponseBodyAsString(),
+ ex.toString(), request.getDescription(false)), HttpStatus.BAD_GATEWAY);
+ } else {
+ return new ResponseEntity<>(new ErrorTransport(500, ex), HttpStatus.BAD_GATEWAY);
+ }