mirror of https://github.com/halo-dev/halo
Refactor common error render
parent
5502463660
commit
f3a42147b9
|
@ -2,13 +2,22 @@ package run.halo.app.controller.core;
|
||||||
|
|
||||||
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.text.StrBuilder;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
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.servlet.error.ErrorAttributes;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import run.halo.app.service.ThemeService;
|
import run.halo.app.service.ThemeService;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error page Controller
|
* Error page Controller
|
||||||
|
@ -18,18 +27,25 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Controller
|
@Controller
|
||||||
public class CommonController implements ErrorController {
|
@RequestMapping("${server.error.path:${error.path:/error}}")
|
||||||
|
public class CommonController extends AbstractErrorController {
|
||||||
|
|
||||||
private static final String ERROR_PATH = "/error";
|
private static final String NOT_FOUND_TEMPLATE = "404.ftl";
|
||||||
|
|
||||||
private static final String NOT_FROUND_TEMPLATE = "404.ftl";
|
|
||||||
|
|
||||||
private static final String INTERNAL_ERROR_TEMPLATE = "500.ftl";
|
private static final String INTERNAL_ERROR_TEMPLATE = "500.ftl";
|
||||||
|
|
||||||
|
private static final String ERROR_TEMPLATE = "common/error/error";
|
||||||
|
|
||||||
private final ThemeService themeService;
|
private final ThemeService themeService;
|
||||||
|
|
||||||
public CommonController(ThemeService themeService) {
|
private final ErrorProperties errorProperties;
|
||||||
|
|
||||||
|
public CommonController(ThemeService themeService,
|
||||||
|
ErrorAttributes errorAttributes,
|
||||||
|
ServerProperties serverProperties) {
|
||||||
|
super(errorAttributes);
|
||||||
this.themeService = themeService;
|
this.themeService = themeService;
|
||||||
|
this.errorProperties = serverProperties.getError();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,11 +54,11 @@ public class CommonController implements ErrorController {
|
||||||
* @param request request
|
* @param request request
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = ERROR_PATH)
|
@GetMapping
|
||||||
public String handleError(HttpServletRequest request) {
|
public String handleError(HttpServletRequest request, HttpServletResponse response, Model model) {
|
||||||
final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
HttpStatus status = getStatus(request);
|
||||||
|
|
||||||
log.error("Error path: [{}], status: [{}]", getErrorPath(), statusCode);
|
log.error("Error path: [{}], status: [{}]", getErrorPath(), status);
|
||||||
|
|
||||||
// Get the exception
|
// Get the exception
|
||||||
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
|
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
|
||||||
|
@ -57,7 +73,18 @@ public class CommonController implements ErrorController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return statusCode == 500 ? contentInternalError() : contentNotFround();
|
Map<String, Object> errorDetail = Collections.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request)));
|
||||||
|
model.addAttribute("error", errorDetail);
|
||||||
|
|
||||||
|
log.debug("Error detail: [{}]", errorDetail);
|
||||||
|
|
||||||
|
if (status.equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
|
||||||
|
return contentInternalError();
|
||||||
|
} else if (status.equals(HttpStatus.NOT_FOUND)) {
|
||||||
|
return contentNotFround();
|
||||||
|
} else {
|
||||||
|
return defaultErrorHandler();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,8 +94,8 @@ public class CommonController implements ErrorController {
|
||||||
*/
|
*/
|
||||||
@GetMapping(value = "/404")
|
@GetMapping(value = "/404")
|
||||||
public String contentNotFround() {
|
public String contentNotFround() {
|
||||||
if (!themeService.templateExists(NOT_FROUND_TEMPLATE)) {
|
if (!themeService.templateExists(NOT_FOUND_TEMPLATE)) {
|
||||||
return "common/error/404";
|
return defaultErrorHandler();
|
||||||
}
|
}
|
||||||
StrBuilder path = new StrBuilder("themes/");
|
StrBuilder path = new StrBuilder("themes/");
|
||||||
path.append(themeService.getActivatedTheme().getFolderName());
|
path.append(themeService.getActivatedTheme().getFolderName());
|
||||||
|
@ -84,14 +111,19 @@ public class CommonController implements ErrorController {
|
||||||
@GetMapping(value = "/500")
|
@GetMapping(value = "/500")
|
||||||
public String contentInternalError() {
|
public String contentInternalError() {
|
||||||
if (!themeService.templateExists(INTERNAL_ERROR_TEMPLATE)) {
|
if (!themeService.templateExists(INTERNAL_ERROR_TEMPLATE)) {
|
||||||
return "common/error/500";
|
return defaultErrorHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
StrBuilder path = new StrBuilder("themes/");
|
StrBuilder path = new StrBuilder("themes/");
|
||||||
path.append(themeService.getActivatedTheme().getFolderName());
|
path.append(themeService.getActivatedTheme().getFolderName());
|
||||||
path.append("/500");
|
path.append("/500");
|
||||||
return path.toString();
|
return path.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String defaultErrorHandler() {
|
||||||
|
return ERROR_TEMPLATE;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the path of the error page.
|
* Returns the path of the error page.
|
||||||
*
|
*
|
||||||
|
@ -99,6 +131,23 @@ public class CommonController implements ErrorController {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String getErrorPath() {
|
public String getErrorPath() {
|
||||||
return ERROR_PATH;
|
return this.errorProperties.getPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine if the stacktrace attribute should be included.
|
||||||
|
*
|
||||||
|
* @param request the source request
|
||||||
|
* @return if the stacktrace attribute should be included
|
||||||
|
*/
|
||||||
|
protected boolean isIncludeStackTrace(HttpServletRequest request) {
|
||||||
|
ErrorProperties.IncludeStacktrace include = errorProperties.getIncludeStacktrace();
|
||||||
|
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
|
||||||
|
return getTraceParameter(request);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
||||||
|
<link rel="alternate" type="application/rss+xml" title="atom 1.0" href="/atom.xml">
|
||||||
|
<title>Not Found</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="page_error">
|
||||||
|
<h1>${error.status}</h1>
|
||||||
|
<p>The page you are looking for is missing</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue