2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.ric.portal.dashboard.controller;
23 import java.lang.invoke.MethodHandles;
26 import javax.servlet.http.HttpServletRequest;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.boot.web.servlet.error.ErrorAttributes;
32 import org.springframework.boot.web.servlet.error.ErrorController;
33 import org.springframework.http.MediaType;
34 import org.springframework.stereotype.Controller;
35 import org.springframework.web.bind.annotation.GetMapping;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.context.request.ServletWebRequest;
38 import springfox.documentation.annotations.ApiIgnore;
41 * Provides a controller which is invoked on any error within the Spring-managed
42 * context, including page not found, and redirects the caller to a custom error
43 * page. The caller is also redirected to this page if a REST controller takes
44 * an uncaught exception.
46 * If trace is requested via request parameter ("?trace=true") and available,
47 * adds stack trace information to the standard JSON error response.
49 * Excluded from Swagger API documentation.
51 * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page
52 * https://www.baeldung.com/spring-boot-custom-error-page
57 @RequestMapping(value = SimpleErrorController.ERROR_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
58 public class SimpleErrorController implements ErrorController {
60 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
62 @SuppressWarnings("squid:S1075")
63 public static final String ERROR_PATH = "/error";
65 private final ErrorAttributes errorAttributes;
68 public SimpleErrorController(ErrorAttributes errorAttributes) {
69 this.errorAttributes = errorAttributes;
73 public String getErrorPath() {
74 logger.warn("getErrorPath");
79 public String handleError(HttpServletRequest request) {
80 ServletWebRequest servletWebRequest = new ServletWebRequest(request);
81 Throwable t = errorAttributes.getError(servletWebRequest);
83 logger.warn("handleError", t);
84 Map<String, Object> attributes = errorAttributes.getErrorAttributes(servletWebRequest, true);
85 attributes.forEach((attribute, value) -> logger.warn("handleError: {} -> {}", attribute, value));
86 // Return the name of the page INCLUDING suffix, which I guess is a "view" name.
87 // Just "error" is not enough, but don't seem to need a ModelAndView object.