Improved error printouts
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / controller / CustomResponseEntityExceptionHandler.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.controller;
22
23 import java.lang.invoke.MethodHandles;
24
25 import org.oransc.portal.nonrtric.controlpanel.model.ErrorTransport;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.http.HttpStatus;
29 import org.springframework.http.ResponseEntity;
30 import org.springframework.web.bind.annotation.ControllerAdvice;
31 import org.springframework.web.bind.annotation.ExceptionHandler;
32 import org.springframework.web.client.HttpStatusCodeException;
33 import org.springframework.web.client.RestClientResponseException;
34 import org.springframework.web.context.request.WebRequest;
35 import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
36
37 /**
38  * Catches certain exceptions. This controller advice factors out try-catch
39  * blocks in many controller methods.
40  *
41  * Also see:<br>
42  * https://www.baeldung.com/exception-handling-for-rest-with-spring
43  * https://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services
44  */
45 @ControllerAdvice
46 public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
47
48     // Superclass has "logger" that is exposed here, so use a different name
49     private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
50
51     /**
52      * Logs the error and generates a JSON response when a REST controller method
53      * takes a RestClientResponseException. This is thrown by the Http client when a
54      * remote method returns a non-2xx code. All the controller methods are proxies
55      * in that they just forward the request along to a remote system, so if that
56      * remote system fails, return 502 plus some details about the failure, rather
57      * than the generic 500 that Spring-Boot will return on an uncaught exception.
58      *
59      * Why 502? I quote: <blockquote>HTTP server received an invalid response from a
60      * server it consulted when acting as a proxy or gateway.</blockquote>
61      *
62      * @param ex
63      *        The exception
64      *
65      * @param request
66      *        The original request
67      *
68      * @return A response entity with status code 502 plus some details in the body.
69      */
70     @ExceptionHandler({RestClientResponseException.class})
71     public final ResponseEntity<ErrorTransport> handleProxyMethodException(Exception ex, WebRequest request) {
72         // Capture the full stack trace in the log.
73         log.error("handleProxyMethodException: request {}, exception {}", request.getDescription(false),
74             ex.getMessage());
75         if (ex instanceof HttpStatusCodeException) {
76             HttpStatusCodeException hsce = (HttpStatusCodeException) ex;
77             return new ResponseEntity<>(new ErrorTransport(hsce.getRawStatusCode(), hsce.getResponseBodyAsString(),
78                 ex.toString(), request.getDescription(false)), HttpStatus.BAD_GATEWAY);
79         } else {
80             return new ResponseEntity<>(new ErrorTransport(500, ex), HttpStatus.BAD_GATEWAY);
81         }
82     }
83
84 }