X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=inline;f=webapp-backend%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fric%2Fportal%2Fdashboard%2Fcontroller%2FSimpleErrorController.java;h=ca3df2214181cea067bc4092dc7b620c577f8bd1;hb=bf91f764c67001c4cad28075a38fd9196744c041;hp=a1678be0bc1dec0db84a9c78bd215c815fbe15d0;hpb=be7a01eda7e4fb9f97a18009d727e6a0692e9399;p=portal%2Fric-dashboard.git diff --git a/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/SimpleErrorController.java b/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/SimpleErrorController.java index a1678be0..ca3df221 100644 --- a/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/SimpleErrorController.java +++ b/webapp-backend/src/main/java/org/oransc/ric/portal/dashboard/controller/SimpleErrorController.java @@ -17,84 +17,77 @@ * limitations under the License. * ========================LICENSE_END=================================== */ + package org.oransc.ric.portal.dashboard.controller; +import java.lang.invoke.MethodHandles; import java.util.Map; import javax.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.boot.web.servlet.error.ErrorController; -import org.springframework.util.Assert; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.ServletWebRequest; -import org.springframework.web.context.request.WebRequest; import springfox.documentation.annotations.ApiIgnore; /** - * Returns JSON on error within the Spring-managed context. Does not fire for - * anything else; e.g., resource not found outside the context. If trace is - * requested via request parameter ("?trace=true") and available, adds stack - * trace information to the standard JSON error response. + * Provides a controller which is invoked on any error within the Spring-managed + * context, including page not found, and redirects the caller to a custom error + * page. The caller is also redirected to this page if a REST controller takes + * an uncaught exception. + * + * If trace is requested via request parameter ("?trace=true") and available, + * adds stack trace information to the standard JSON error response. * * Excluded from Swagger API documentation. * * https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page * https://www.baeldung.com/spring-boot-custom-error-page */ + @ApiIgnore -@RestController +@Controller +@RequestMapping(value = SimpleErrorController.ERROR_PATH, produces = MediaType.APPLICATION_JSON_VALUE) public class SimpleErrorController implements ErrorController { - private static final String ERROR_PATH = "/error"; - private static final String TRACE = "trace"; + private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public static final String ERROR_PATH = "/error"; + private final ErrorAttributes errorAttributes; - /** - * Constructor - * - * @param errorAttributes - * error attributes must not be null - */ @Autowired public SimpleErrorController(ErrorAttributes errorAttributes) { - Assert.notNull(errorAttributes, "ErrorAttributes must not be null"); this.errorAttributes = errorAttributes; } @Override public String getErrorPath() { + logger.warn("getErrorPath"); return ERROR_PATH; } - /** - * Builds a map with error details - * - * @param aRequest - * HttpServletRequest - * @return Map of String to Object - */ - @RequestMapping(ERROR_PATH) - public Map error(HttpServletRequest aRequest) { - Map body = getErrorAttributes(aRequest, getTraceParameter(aRequest)); - body.put("decorated-by", SimpleErrorController.class.getName()); - body.computeIfPresent(TRACE, (key, value) -> body.put(TRACE, ((String) value).split("\n\t"))); - return body; - } - - private boolean getTraceParameter(HttpServletRequest request) { - String parameter = request.getParameter(TRACE); - if (parameter == null) - return false; - return !"false".equalsIgnoreCase(parameter); - } - - private Map getErrorAttributes(HttpServletRequest aRequest, boolean includeStackTrace) { - WebRequest webRequest = new ServletWebRequest(aRequest); - return errorAttributes.getErrorAttributes(webRequest, includeStackTrace); + @GetMapping + public String handleError(HttpServletRequest request) { + ServletWebRequest servletWebRequest = new ServletWebRequest(request); + Throwable t = errorAttributes.getError(servletWebRequest); + if (t != null) + logger.warn("handleError", t); + Map attributes = errorAttributes.getErrorAttributes(servletWebRequest, true); + attributes.forEach((attribute, value) -> { + logger.warn("handleError: {} -> {}", attribute, value); + }); + // Return the name of the page INCLUDING suffix, which I guess is a "view" name. + // Just "error" is not enough, but don't seem to need a ModelAndView object. + return "error.html"; } -} +} \ No newline at end of file