2 * ===============LICENSE_START=======================================================
4 * ===================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6 * ===================================================================================
7 * This Acumos software file is distributed by AT&T and Tech Mahindra
8 * under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * This file is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ===============LICENSE_END=========================================================
20 package org.oransc.ric.portal.dashboard.controller;
22 import java.lang.invoke.MethodHandles;
24 import org.oransc.ric.portal.dashboard.model.ErrorTransport;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.http.HttpStatus;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.web.bind.annotation.ControllerAdvice;
30 import org.springframework.web.bind.annotation.ExceptionHandler;
31 import org.springframework.web.client.HttpStatusCodeException;
32 import org.springframework.web.context.request.WebRequest;
33 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
36 * Catches Http status code exceptions and builds a response with code 502 and
37 * some details wrapped in an ErrorTransport object. This factors out try-catch
38 * blocks in many controller methods.
40 * Why 502? I quote: <blockquote>HTTP server received an invalid response from a
41 * server it consulted when acting as a proxy or gateway.</blockquote>
43 * This class and the methods are not strictly necessary, the
44 * SimpleErrorController is invoked when any controller method takes an uncaught
45 * exception, but this approach provides a better response to the front end and
46 * doesn't signal internal server error.
49 * https://www.baeldung.com/exception-handling-for-rest-with-spring
50 * https://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services
53 public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
55 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58 * Generates the response when a REST controller method takes an
59 * HttpStatusCodeException. Confusingly, the container first redirects to /error
61 * {@link org.oransc.ric.portal.dashboard.controller.SimpleErrorController}
62 * method, and that response arrives here as the response body.
68 * @return A response entity with status code 502 plus some details in the body.
70 @ExceptionHandler(HttpStatusCodeException.class)
71 public final ResponseEntity<?> handleHttpStatusCodeException(HttpStatusCodeException ex, WebRequest request) {
72 logger.warn("Request {} failed, status code {}", request.getDescription(false), ex.getStatusCode());
73 return new ResponseEntity<ErrorTransport>(
74 new ErrorTransport(ex.getRawStatusCode(), ex.getResponseBodyAsString(), ex), HttpStatus.BAD_GATEWAY);