Release dashboard image at version 2.1.0
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / CustomResponseEntityExceptionHandler.java
index a881616..f4a873d 100644 (file)
@@ -21,8 +21,9 @@ package org.oransc.ric.portal.dashboard.controller;
 
 import java.lang.invoke.MethodHandles;
 
+import org.oransc.ric.portal.dashboard.exception.InvalidArgumentException;
+import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
 import org.oransc.ric.portal.dashboard.exception.UnknownInstanceException;
-import org.oransc.ric.portal.dashboard.model.ErrorTransport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.http.HttpStatus;
@@ -48,6 +49,19 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
        // Superclass has "logger" that is exposed here, so use a different name
        private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
+       /**
+        * Generates a message with an upper limit on size.
+        * 
+        * @param t
+        *              Throwable
+        * @return Message
+        */
+       private String getShortExceptionMessage(Throwable t) {
+               final int enough = 256;
+               String exString = t.toString();
+               return exString.length() > enough ? exString.substring(0, enough) : exString;
+       }
+
        /**
         * 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
@@ -65,35 +79,67 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio
         * @param request
         *                    The original request
         * 
-        * @return A response entity with status code 502 plus some details in the body.
+        * @return A response entity with status code 502 and an unstructured message.
         */
        @ExceptionHandler({ RestClientResponseException.class })
-       public final ResponseEntity<ErrorTransport> handleProxyMethodException(Exception ex, WebRequest request) {
+       public final ResponseEntity<String> handleRestClientResponse(Exception ex, WebRequest request) {
                // Capture the full stack trace in the log.
-               log.error("handleProxyMethodException: request {}, exception {}", request.getDescription(false), ex);
+               if (log.isErrorEnabled())
+                       log.error("handleRestClientResponse: request " + 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);
+                       return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(hsce.getResponseBodyAsString());
                } else {
-                       return new ResponseEntity<>(new ErrorTransport(500, ex), HttpStatus.BAD_GATEWAY);
+                       return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(getShortExceptionMessage(ex));
                }
        }
 
        /**
-        * Logs a warning if an invalid RIC instance key is used.
+        * Logs the error and generates a response when a REST controller method takes
+        * an UnknownInstanceException, an invalid RIC instance key was used.
         * 
         * @param ex
         *                    The exception
         * @param request
         *                    The original request
-        * @return A response entity with status code 400
+        * @return A response entity with status code 400 and an unstructured message.
         */
        @ExceptionHandler({ UnknownInstanceException.class })
-       public final ResponseEntity<ErrorTransport> handleUnknownInstanceException(Exception ex, WebRequest request) {
-               log.warn("handleUnknownInstanceException: request {}, exception {}", request.getDescription(false),
-                               ex.toString());
-               return new ResponseEntity<>(new ErrorTransport(400, ex), HttpStatus.BAD_REQUEST);
+       public final ResponseEntity<String> handleUnknownInstance(Exception ex, WebRequest request) {
+               log.warn("handleUnknownInstance: request {}, exception {}", request.getDescription(false), ex.toString());
+               return ResponseEntity.badRequest().body(getShortExceptionMessage(ex));
+       }
+
+       /**
+        * Logs the error and generates a response when a REST controller method takes
+        * an InvalidArgumentException, an invalid JSON was sent.
+        * 
+        * @param ex
+        *                    The exception
+        * @param request
+        *                    The original request
+        * @return A response entity with status code 400 and an unstructured message.
+        */
+       @ExceptionHandler({ InvalidArgumentException.class })
+       public final ResponseEntity<String> handleInvalidArgument(Exception ex, WebRequest request) {
+               log.warn("handleInvalidArgument: request {}, exception {}", request.getDescription(false), ex.toString());
+               return ResponseEntity.badRequest().body(getShortExceptionMessage(ex));
+       }
+
+       /**
+        * Logs the error and generates a response when a REST controller method takes
+        * an StatsManagerException.
+        * 
+        * @param ex
+        *                    The exception
+        * @param request
+        *                    The original request
+        * @return A response entity with status code 400 and an unstructured message.
+        */
+       @ExceptionHandler({ StatsManagerException.class })
+       public final ResponseEntity<String> handleStatsManager(Exception ex, WebRequest request) {
+               log.warn("handleStatsManager: request {}, exception {}", request.getDescription(false), ex.toString());
+               return ResponseEntity.badRequest().body(getShortExceptionMessage(ex));
        }
 
 }