pref: 添加错误码&修改过时方法 (#1089)

pull/1095/head
Dylan·Yuan 2020-09-30 00:15:55 +08:00 committed by GitHub
parent d3953fe864
commit 868efecdce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
@ -79,7 +80,9 @@ public class CommonController extends AbstractErrorController {
handleCustomException(request);
Map<String, Object> errorDetail = Collections.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request)));
ErrorAttributeOptions options = getErrorAttributeOptions(request);
Map<String, Object> errorDetail = Collections.unmodifiableMap(getErrorAttributes(request, options));
model.addAttribute("error", errorDetail);
model.addAttribute("meta_keywords", optionService.getSeoKeywords());
model.addAttribute("meta_description", optionService.getSeoDescription());
@ -87,6 +90,7 @@ public class CommonController extends AbstractErrorController {
HttpStatus status = getStatus(request);
response.setStatus(status.value());
if (status.equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
return contentInternalError();
} else if (status.equals(HttpStatus.NOT_FOUND)) {
@ -209,4 +213,21 @@ public class CommonController extends AbstractErrorController {
}
return false;
}
/**
* Get the ErrorAttributeOptions .
*
* @param request the source request
* @return {@link ErrorAttributeOptions}
*/
private ErrorAttributeOptions getErrorAttributeOptions(HttpServletRequest request) {
ErrorProperties.IncludeStacktrace include = errorProperties.getIncludeStacktrace();
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
return ErrorAttributeOptions.of(ErrorAttributeOptions.Include.STACK_TRACE);
}
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM && getTraceParameter(request)) {
return ErrorAttributeOptions.of(ErrorAttributeOptions.Include.STACK_TRACE);
}
return ErrorAttributeOptions.defaults();
}
}