X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=dashboard%2Fwebapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2Fcontroller%2FCustomResponseEntityExceptionHandler.java;h=f4a873d8d5c1936c602d35341fe9b08877dd5a81;hb=848677173191182d46aa22ab83bee0de84999a81;hp=a881616226d1905d56e20523e4abe8f199345136;hpb=64a5e9470799236f0af4ce2df98f77c94eb1bed3;p=portal%2Fric-dashboard.git diff --git a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/CustomResponseEntityExceptionHandler.java b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/CustomResponseEntityExceptionHandler.java index a8816162..f4a873d8 100644 --- a/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/CustomResponseEntityExceptionHandler.java +++ b/dashboard/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/CustomResponseEntityExceptionHandler.java @@ -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 handleProxyMethodException(Exception ex, WebRequest request) { + public final ResponseEntity 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 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 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 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 handleStatsManager(Exception ex, WebRequest request) { + log.warn("handleStatsManager: request {}, exception {}", request.getDescription(false), ex.toString()); + return ResponseEntity.badRequest().body(getShortExceptionMessage(ex)); } }