异常处理
This commit is contained in:
parent
f0d3c01f8b
commit
33bd1c55be
|
@ -0,0 +1,77 @@
|
|||
package io.metersphere.controller.handler;
|
||||
|
||||
import io.metersphere.controller.ResultHolder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
|
||||
@RestController
|
||||
public class GlobalExceptionHandler implements ErrorController {
|
||||
|
||||
public static final org.slf4j.Logger Logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||
|
||||
private static final String PATH = "/error";
|
||||
|
||||
@Resource
|
||||
private ErrorAttributes errorAttributes;
|
||||
|
||||
@Override
|
||||
public String getErrorPath() {
|
||||
return PATH;
|
||||
}
|
||||
|
||||
@RequestMapping(value = {PATH}, produces = {"text/html"})
|
||||
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
|
||||
return new ModelAndView("error", this.getErrorAttributes(request, false));
|
||||
}
|
||||
|
||||
@RequestMapping(value = PATH)
|
||||
public ResultHolder error(HttpServletRequest request, HttpServletResponse response) {
|
||||
WebRequest webRequest = new ServletWebRequest(request);
|
||||
Throwable t = errorAttributes.getError(webRequest);
|
||||
Map<String, Object> errorAttributeMap = errorAttributes.getErrorAttributes(webRequest, true);
|
||||
Integer code = (Integer) errorAttributeMap.get("status");
|
||||
response.setStatus(code);
|
||||
String errorMessage = StringUtils.EMPTY;
|
||||
if (t != null) {
|
||||
if (Logger.isDebugEnabled()) {
|
||||
Logger.error("Fail to proceed " + errorAttributeMap.get("path"), t);
|
||||
}
|
||||
errorMessage = t.getMessage();
|
||||
}
|
||||
if (StringUtils.isBlank(errorMessage)) {
|
||||
if (code == 403) {
|
||||
errorMessage = "Permission Denied.";
|
||||
} else if (code == 404) {
|
||||
String path = request.getServletPath();
|
||||
if (Objects.nonNull(request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI)) && StringUtils.isNotBlank(request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI).toString())) {
|
||||
path = request.getAttribute(RequestDispatcher.ERROR_REQUEST_URI).toString();
|
||||
}
|
||||
errorMessage = path + " not found.";
|
||||
} else {
|
||||
errorMessage = "The server responds " + code + " but no detailed message.";
|
||||
}
|
||||
}
|
||||
return ResultHolder.error(errorMessage);
|
||||
}
|
||||
|
||||
protected Map<String, Object> getErrorAttributes(HttpServletRequest request, boolean includeStackTrace) {
|
||||
WebRequest webRequest = new ServletWebRequest(request);
|
||||
return this.errorAttributes.getErrorAttributes(webRequest, includeStackTrace);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package io.metersphere.controller.handler;
|
||||
|
||||
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.controller.ResultHolder;
|
||||
import org.apache.shiro.ShiroException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
||||
@RestControllerAdvice
|
||||
public class RestControllerExceptionHandler {
|
||||
/*=========== Shiro 异常拦截==============*/
|
||||
@ExceptionHandler(ShiroException.class)
|
||||
public ResultHolder exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
||||
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
||||
return ResultHolder.error(exception.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler(MSException.class)
|
||||
public ResultHolder f2cExceptionHandler(HttpServletRequest request, HttpServletResponse response, MSException e) {
|
||||
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
|
||||
return ResultHolder.error(e.getMessage());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue