X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dashboard%2Fwebapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2Fcontroller%2FCustomResponseEntityExceptionHandler.java;h=e75dfcb16243b933334872ec7ed573da369bd7d4;hb=e0adfb63c5c6eb10bcea3bdf0d080e48aea38a51;hp=a881616226d1905d56e20523e4abe8f199345136;hpb=d67a8cb18d802e6b638b5e76c4152fb790cc3126;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..e75dfcb1 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 @@ -22,7 +22,6 @@ package org.oransc.ric.portal.dashboard.controller; import java.lang.invoke.MethodHandles; 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 +47,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,18 +77,17 @@ 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 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); + 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)); } } @@ -87,13 +98,13 @@ public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptio * 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) { + 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); + return ResponseEntity.badRequest().body(getShortExceptionMessage(ex)); } }