From 868efecdce783a81e661e1f116fb780c31d8d26a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dylan=C2=B7Yuan?= <986641202@qq.com> Date: Wed, 30 Sep 2020 00:15:55 +0800 Subject: [PATCH] =?UTF-8?q?pref:=20=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E7=A0=81&=E4=BF=AE=E6=94=B9=E8=BF=87=E6=97=B6=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20(#1089)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/controller/core/CommonController.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/run/halo/app/controller/core/CommonController.java b/src/main/java/run/halo/app/controller/core/CommonController.java index 272044baf..517336519 100644 --- a/src/main/java/run/halo/app/controller/core/CommonController.java +++ b/src/main/java/run/halo/app/controller/core/CommonController.java @@ -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 errorDetail = Collections.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request))); + ErrorAttributeOptions options = getErrorAttributeOptions(request); + + Map 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(); + } }