6d8afa7deb6833eaab79c387e86097ba062a411c
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / CustomResponseEntityExceptionHandler.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License 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===================================
19  */
20 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23
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;
34
35 /**
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.
39  * 
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>
42  * 
43  * Also see:<br>
44  * https://www.baeldung.com/exception-handling-for-rest-with-spring
45  * https://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services
46  */
47 @ControllerAdvice
48 public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
49
50         // Superclass has "logger" that is exposed here, so use a different name
51         private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52
53         /*
54          * Generates the response when a REST controller method takes an
55          * HttpStatusCodeException.
56          * 
57          * @param ex The exception
58          * 
59          * @param request The original request
60          * 
61          * @return A response entity with status code 502 plus some details in the body.
62          */
63         @ExceptionHandler(HttpStatusCodeException.class)
64         public final ResponseEntity<ErrorTransport> handleHttpStatusCodeException(HttpStatusCodeException ex,
65                         WebRequest request) {
66                 log.warn("handleHttpStatusCodeException: request {}, status code {}", request.getDescription(false),
67                                 ex.getStatusCode());
68                 return new ResponseEntity<>(new ErrorTransport(ex.getRawStatusCode(), ex.getResponseBodyAsString(), ex),
69                                 HttpStatus.BAD_GATEWAY);
70         }
71
72 }