| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package cn.com.goldenwater.dcproj.interceptor;
- import cn.com.goldenwater.core.exception.BaseException;
- import cn.com.goldenwater.core.web.BaseController;
- import cn.com.goldenwater.core.web.BaseResponse;
- import cn.com.goldenwater.dcproj.service.ErrorService;
- import cn.com.goldenwater.target.CheckException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.TypeMismatchException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.HttpRequestMethodNotSupportedException;
- import org.springframework.web.bind.MissingServletRequestParameterException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
- import org.springframework.web.servlet.NoHandlerFoundException;
- import javax.servlet.http.HttpServletRequest;
- @ControllerAdvice
- @ResponseBody
- public class GlobalExceptionHandler extends BaseController {
- private Logger logger = LoggerFactory.getLogger(this.getClass());
- @Value("${intercept.enable}")
- private boolean interceptEnable;
- @Autowired
- private ErrorService errorService;
- @ExceptionHandler({MethodArgumentTypeMismatchException.class})
- public Object handleMethodArgumentTypeMismatch(HttpServletRequest req, MethodArgumentTypeMismatchException e) {
- e.printStackTrace();
- this.logger.error("MethodArgumentTypeMismatchException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
- if (interceptEnable) {
- errorService.addThrowable(e, req);
- }
- return buildFailResponse(40020, "参数类型不匹配,参数" + e.getName() + "类型应该为" + e.getRequiredType());
- }
- @ExceptionHandler({TypeMismatchException.class})
- public Object handleTypeMismatch(HttpServletRequest req, TypeMismatchException e) {
- e.printStackTrace();
- this.logger.error("TypeMismatchException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
- if (interceptEnable) {
- errorService.addThrowable(e, req);
- }
- return buildFailResponse(40021, "参数类型不匹配,参数" + e.getPropertyName() + "类型应该为" + e.getRequiredType());
- }
- @ExceptionHandler({MissingServletRequestParameterException.class})
- public Object handleMissingServletRequestParameter(HttpServletRequest req, MissingServletRequestParameterException e) {
- e.printStackTrace();
- this.logger.error("MissingServletRequestParameterException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
- if (interceptEnable) {
- errorService.addThrowable(e, req);
- }
- return buildFailResponse(40022, "缺少必要参数,参数名称为" + e.getParameterName());
- }
- @ExceptionHandler({BaseException.class})
- public Object handleBaseException(HttpServletRequest req, Exception e) {
- e.printStackTrace();
- this.logger.error("BaseException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
- return buildFailResponse(4003, "请求路径不存在");
- }
- @ExceptionHandler({NoHandlerFoundException.class})
- public Object handleNotFound(HttpServletRequest req, Exception e) {
- e.printStackTrace();
- this.logger.error("NoHandlerFoundException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
- return buildFailResponse(4003, "请求路径不存在");
- }
- @ExceptionHandler({Exception.class})
- public Object handleDefault(HttpServletRequest req, Exception e) {
- e.printStackTrace();
- this.logger.error("DefaultException - uri: {} error: {} exception: {}", req.getRequestURI(), e.getMessage(), e.getClass().getName());
- if (interceptEnable) {
- errorService.addThrowable(e, req);
- }
- return buildFailResponse(4004, "系统错误,请联系技术人员!");
- }
- @ExceptionHandler(value = CheckException.class)
- public BaseResponse checkErrorHandler(CheckException e, HttpServletRequest req) {
- e.printStackTrace();
- this.logger.error("DefaultException - uri: {} error: {} exception: {}", req.getRequestURI(), e.getMessage(), e.getClass().getName());
- if (interceptEnable) {
- errorService.addThrowable(e, req);
- }
- return buildFailResponse(4001, e.getMessage());
- }
- @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
- public BaseResponse HttpRequestMethodNotSupportedExceptionHandler(CheckException e, HttpServletRequest req) {
- e.printStackTrace();
- if (interceptEnable) {
- this.logger.error("HttpRequestMethodNotSupportedException - uri: {} error: {}", req.getRequestURI(), e.getMessage(), e);
- errorService.addThrowable(e, req);
- }
- return buildFailResponse(4005, e.getMessage());
- }
- }
|