mirror of https://github.com/halo-dev/halo
Redirect 404 if view render is not found
parent
84740fdfea
commit
7e3f584843
|
@ -11,14 +11,13 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
|
|||
import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice;
|
||||
|
||||
/**
|
||||
* Controller adivce for comment result.
|
||||
* Controller advice for comment result.
|
||||
*
|
||||
* @author johnniang
|
||||
*/
|
||||
@ControllerAdvice("cc.ryanc.halo.web.controller.api")
|
||||
public class CommonResultControllerAdvice extends AbstractMappingJacksonResponseBodyAdvice {
|
||||
|
||||
|
||||
@Override
|
||||
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer,
|
||||
MediaType contentType,
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
package cc.ryanc.halo.web.controller.core;
|
||||
|
||||
import cc.ryanc.halo.logging.Logger;
|
||||
import cc.ryanc.halo.model.enums.CommonParamsEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -16,10 +17,11 @@ import javax.servlet.http.HttpServletRequest;
|
|||
* @author : RYAN0UP
|
||||
* @date : 2017/12/26
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class CommonController implements ErrorController {
|
||||
|
||||
private final Logger log = Logger.getLogger(getClass());
|
||||
|
||||
private static final String ERROR_PATH = "/error";
|
||||
|
||||
/**
|
||||
|
@ -31,6 +33,18 @@ public class CommonController implements ErrorController {
|
|||
@GetMapping(value = ERROR_PATH)
|
||||
public String handleError(HttpServletRequest request) {
|
||||
final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||
|
||||
log.error("Error path: [{}], status: [{}]", request.getRequestURI(), statusCode);
|
||||
|
||||
// Get the exception
|
||||
Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
|
||||
|
||||
if (StringUtils.startsWithIgnoreCase(throwable.getMessage(), "Could not resolve view with name '")) {
|
||||
// TODO May cause unreasoned problem
|
||||
// if Ftl was not found then redirect to /404
|
||||
return "redirect:/404";
|
||||
}
|
||||
|
||||
if (statusCode.equals(CommonParamsEnum.NOT_FOUND.getValue())) {
|
||||
return "redirect:/404";
|
||||
} else {
|
||||
|
|
|
@ -59,4 +59,7 @@ spring:
|
|||
messages:
|
||||
basename: i18n/messages
|
||||
logging:
|
||||
level:
|
||||
cc.ryanc.halo: DEBUG
|
||||
org.hibernate: ERROR
|
||||
file: ./logs/log.log
|
||||
|
|
Loading…
Reference in New Issue