Revise controller error handling
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / CustomResponseEntityExceptionHandler.java
index 062d04f..2c432d5 100644 (file)
@@ -29,22 +29,14 @@ import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.client.HttpStatusCodeException;
+import org.springframework.web.client.RestClientResponseException;
 import org.springframework.web.context.request.WebRequest;
 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
 
 /**
- * Catches Http status code exceptions and builds a response with code 502 and
- * some details wrapped in an ErrorTransport object. This factors out try-catch
+ * Catches certain exceptions. This controller advice factors out try-catch
  * blocks in many controller methods.
  * 
- * Why 502? I quote: <blockquote>HTTP server received an invalid response from a
- * server it consulted when acting as a proxy or gateway.</blockquote>
- *
- * This class and the methods are not strictly necessary, the
- * SimpleErrorController is invoked when any controller method takes an uncaught
- * exception, but this approach provides a better response to the front end and
- * doesn't signal internal server error.
- * 
  * Also see:<br>
  * https://www.baeldung.com/exception-handling-for-rest-with-spring
  * https://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services
@@ -56,24 +48,35 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
        private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
        /**
-        * Generates the response when a REST controller method takes an
-        * HttpStatusCodeException. Confusingly, the container first redirects to /error
-        * which invokes the
-        * {@link org.oransc.ric.portal.dashboard.controller.SimpleErrorController}
-        * method, and that response arrives here as the response body.
+        * Logs the error and generates a JSON response when a REST controller method
+        * takes a RestClientResponseException. This is thrown by the Http client when a
+        * remote method returns a non-2xx code. All the controller methods are proxies
+        * in that they just forward the request along to a remote system, so if that
+        * remote system fails, return 502 plus some details about the failure, rather
+        * than the generic 500 that Spring-Boot will return on an uncaught exception.
+        * 
+        * Why 502? I quote: <blockquote>HTTP server received an invalid response from a
+        * server it consulted when acting as a proxy or gateway.</blockquote>
         * 
         * @param ex
         *                    The exception
+        * 
         * @param request
-        *                    The orignal request
+        *                    The original request
+        * 
         * @return A response entity with status code 502 plus some details in the body.
         */
-       @ExceptionHandler(HttpStatusCodeException.class)
-       public final ResponseEntity<ErrorTransport> handleHttpStatusCodeException(HttpStatusCodeException ex,
-                       WebRequest request) {
-               log.warn("Request {} failed, status code {}", request.getDescription(false), ex.getStatusCode());
-               return new ResponseEntity<>(new ErrorTransport(ex.getRawStatusCode(), ex.getResponseBodyAsString(), ex),
-                               HttpStatus.BAD_GATEWAY);
+       @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);
+               }
        }
 
 }