3102e4e14282edecc8c74451f325a92aa129e373
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / SimpleErrorController.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
21 package org.oransc.ric.portal.dashboard.controller;
22
23 import java.lang.invoke.MethodHandles;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.boot.web.servlet.error.ErrorController;
28 import org.springframework.http.MediaType;
29 import org.springframework.stereotype.Controller;
30 import org.springframework.web.bind.annotation.GetMapping;
31 import org.springframework.web.bind.annotation.RequestMapping;
32
33 import springfox.documentation.annotations.ApiIgnore;
34
35 /**
36  * Provides a controller which is invoked on any error within the Spring-managed
37  * context, including page not found, and redirects the caller to a custom error
38  * page. The caller is also redirected to this page if a REST controller takes
39  * an uncaught exception.
40  * 
41  * If trace is requested via request parameter ("?trace=true") and available,
42  * adds stack trace information to the standard JSON error response.
43  * 
44  * Excluded from Swagger API documentation.
45  * 
46  * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
47  * https://www.baeldung.com/spring-boot-custom-error-page
48  */
49
50 @ApiIgnore
51 @Controller
52 @RequestMapping(value = SimpleErrorController.ERROR_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
53 public class SimpleErrorController implements ErrorController {
54
55         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56
57         public static final String ERROR_PATH = "/error";
58
59         @Override
60         public String getErrorPath() {
61                 logger.warn("getErrorPath");
62                 return ERROR_PATH;
63         }
64
65         @GetMapping
66         public String handleError() {
67                 logger.warn("handleError");
68                 // Return the name of the page INCLUDING suffix, which I guess is a "view" name.
69                 // Just "error" is not enough, but don't seem to need a ModelAndView object.
70                 return "error.html";
71         }
72
73 }