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=b113d6446982380f9de5f36611d924c0e5dbe459;hb=a2ad32a98e7a3f32214d3ecd7ca9730e3602d11f;hp=f5ecd1044c7100e2a71b5c711850587fbde7a5f1;hpb=a2bc79c5f0027e953815d6e98814a748b36827aa;p=nonrtric.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 f5ecd104..b113d644 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 @@ -3,13 +3,14 @@ * O-RAN-SC * %% * Copyright (C) 2019 AT&T Intellectual Property + * Modifications Copyright (C) 2020 Nordix Foundation * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -36,7 +37,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep /** * Catches certain exceptions. This controller advice factors out try-catch * blocks in many controller methods. - * + * * Also see:
* https://www.baeldung.com/exception-handling-for-rest-with-spring * https://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services @@ -44,39 +45,40 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep @ControllerAdvice public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { - // Superclass has "logger" that is exposed here, so use a different name - private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + // Superclass has "logger" that is exposed here, so use a different name + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); - /** - * 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:
HTTP server received an invalid response from a - * server it consulted when acting as a proxy or gateway.
- * - * @param ex - * The exception - * - * @param request - * The original request - * - * @return A response entity with status code 502 plus some details in the body. - */ - @ExceptionHandler({ RestClientResponseException.class }) - 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); - } else { - return new ResponseEntity<>(new ErrorTransport(500, ex), HttpStatus.BAD_GATEWAY); - } - } + /** + * 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:
HTTP server received an invalid response from a + * server it consulted when acting as a proxy or gateway.
+ * + * @param ex + * The exception + * + * @param request + * The original request + * + * @return A response entity with status code 502 plus some details in the body. + */ + @ExceptionHandler({RestClientResponseException.class}) + 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.getMessage()); + 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); + } + } }