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