2023年12月18日发(作者:)

import tions;import ;import ;import rvletRequest;import rvletResponse;import roperties;import eStacktrace;import ttributes;import ctServletWebServerFactory;import atus;import ype;import seEntity;import ller;import ;import tMapping;import ndView;/** * Basic global error {@link Controller @Controller}, rendering {@link ErrorAttributes}. * More specific errors can be handled either using Spring MVC abstractions (e.g. * {@code @ExceptionHandler}) or by adding servlet * {@link AbstractServletWebServerFactory#setErrorPages server error pages}. * * @author Dave Syer * @author Phillip Webb * @author Michael Stummvoll * @author Stephane Nicoll * @since 1.0.0 * @see ErrorAttributes * @see ErrorProperties */@Controller//如果没有配置就去找,如果没有配置默认路径为/error@RequestMapping("${:${:/error}}")public class BasicErrorController extends AbstractErrorController { private final ErrorProperties errorProperties; /** * Create a new {@link BasicErrorController} instance. * @param errorAttributes the error attributes * @param errorProperties configuration properties */ public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) { this(errorAttributes, errorProperties, ist()); } /** * Create a new {@link BasicErrorController} instance. * @param errorAttributes the error attributes * @param errorProperties configuration properties * @param errorViewResolvers error view resolvers */ public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) { super(errorAttributes, errorViewResolvers); l(errorProperties, "ErrorProperties must not be null"); roperties = errorProperties; } @Override public String getErrorPath() { return h();

/* * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * /licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package ;import riter;import Writer;import ;import HashMap;import ;import tException;import rvletRequest;import rvletResponse;import d;import ;import atus;import Utils;import gResult;import Error;import ArgumentNotValidException;import tAttributes;import uest;import rExceptionResolver;import ndView;/** * Default implementation of {@link ErrorAttributes}. Provides the following attributes * when possible: *

    *
  • timestamp - The time that the errors were extracted
  • *
  • status - The status code
  • *
  • error - The error reason
  • *
  • exception - The class name of the root exception (if configured)
  • *
  • message - The exception message
  • *
  • errors - Any {@link ObjectError}s from a {@link BindingResult} exception *
  • trace - The exception stack trace
  • *
  • path - The URL path when the exception was raised
  • *
* * @author Phillip Webb * @author Dave Syer * @author Stephane Nicoll * @author Vedran Pavic * @since 2.0.0 * @see ErrorAttributes */@Order(T_PRECEDENCE)public class DefaultErrorAttributes implements ErrorAttributes, HandlerExceptionResolver, Ordered { private static final String ERROR_ATTRIBUTE = e() + ".ERROR";

boolean includeStackTrace) { Throwable error = getError(webRequest); if (error != null) { while (error instanceof ServletException && se() != null) { error = se(); } if (eException) { ("exception", ss().getName()); } addErrorMessage(errorAttributes, error); if (includeStackTrace) { addStackTrace(errorAttributes, error); } } Object message = getAttribute(webRequest, "e"); if ((!y(message) || ("message") == null) && !(error instanceof BindingResult)) { ("message", y(message) ? "No message available" : message); } } private void addErrorMessage(Map errorAttributes, Throwable error) { BindingResult result = extractBindingResult(error); if (result == null) { ("message", sage()); return; } if (ors()) { ("errors", Errors()); ("message", "Validation failed for object='" + ectName() + "'. Error count: " + orCount()); } else { ("message", "No errors"); } } private BindingResult extractBindingResult(Throwable error) { if (error instanceof BindingResult) { return (BindingResult) error; } if (error instanceof MethodArgumentNotValidException) { return ((MethodArgumentNotValidException) error).getBindingResult(); } return null; } private void addStackTrace(Map errorAttributes, Throwable error) { StringWriter stackTrace = new StringWriter(); tackTrace(new PrintWriter(stackTrace)); (); ("trace", ng()); } private void addPath(Map errorAttributes, RequestAttributes requestAttributes) { String path = getAttribute(requestAttributes, "t_uri"); if (path != null) { ("path", path); } } @Override public Throwable getError(WebRequest webRequest) { Throwable exception = getAttribute(webRequest, ERROR_ATTRIBUTE); if (exception == null) { exception = getAttribute(webRequest, "ion");