7e05d64b65592e97c293623a613907dc11db503d.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package cn.com.goldenwater.dcproj.interceptor;
  2. import cn.com.goldenwater.core.exception.BaseException;
  3. import cn.com.goldenwater.core.web.BaseController;
  4. import cn.com.goldenwater.core.web.BaseResponse;
  5. import cn.com.goldenwater.dcproj.service.ErrorService;
  6. import cn.com.goldenwater.target.CheckException;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.TypeMismatchException;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.web.HttpRequestMethodNotSupportedException;
  13. import org.springframework.web.bind.MissingServletRequestParameterException;
  14. import org.springframework.web.bind.annotation.ControllerAdvice;
  15. import org.springframework.web.bind.annotation.ExceptionHandler;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
  18. import org.springframework.web.servlet.NoHandlerFoundException;
  19. import javax.servlet.http.HttpServletRequest;
  20. @ControllerAdvice
  21. @ResponseBody
  22. public class GlobalExceptionHandler extends BaseController {
  23. private Logger logger = LoggerFactory.getLogger(this.getClass());
  24. @Value("${intercept.enable}")
  25. private boolean interceptEnable;
  26. @Autowired
  27. private ErrorService errorService;
  28. @ExceptionHandler({MethodArgumentTypeMismatchException.class})
  29. public Object handleMethodArgumentTypeMismatch(HttpServletRequest req, MethodArgumentTypeMismatchException e) {
  30. e.printStackTrace();
  31. this.logger.error("MethodArgumentTypeMismatchException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
  32. if (interceptEnable) {
  33. errorService.addThrowable(e, req);
  34. }
  35. return buildFailResponse(40020, "参数类型不匹配,参数" + e.getName() + "类型应该为" + e.getRequiredType());
  36. }
  37. @ExceptionHandler({TypeMismatchException.class})
  38. public Object handleTypeMismatch(HttpServletRequest req, TypeMismatchException e) {
  39. e.printStackTrace();
  40. this.logger.error("TypeMismatchException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
  41. if (interceptEnable) {
  42. errorService.addThrowable(e, req);
  43. }
  44. return buildFailResponse(40021, "参数类型不匹配,参数" + e.getPropertyName() + "类型应该为" + e.getRequiredType());
  45. }
  46. @ExceptionHandler({MissingServletRequestParameterException.class})
  47. public Object handleMissingServletRequestParameter(HttpServletRequest req, MissingServletRequestParameterException e) {
  48. e.printStackTrace();
  49. this.logger.error("MissingServletRequestParameterException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
  50. if (interceptEnable) {
  51. errorService.addThrowable(e, req);
  52. }
  53. return buildFailResponse(40022, "缺少必要参数,参数名称为" + e.getParameterName());
  54. }
  55. @ExceptionHandler({BaseException.class})
  56. public Object handleBaseException(HttpServletRequest req, Exception e) {
  57. e.printStackTrace();
  58. this.logger.error("BaseException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
  59. return buildFailResponse(4003, "请求路径不存在");
  60. }
  61. @ExceptionHandler({NoHandlerFoundException.class})
  62. public Object handleNotFound(HttpServletRequest req, Exception e) {
  63. e.printStackTrace();
  64. this.logger.error("NoHandlerFoundException - uri: {} error: {}", req.getRequestURI(), e.getMessage());
  65. return buildFailResponse(4003, "请求路径不存在");
  66. }
  67. @ExceptionHandler({Exception.class})
  68. public Object handleDefault(HttpServletRequest req, Exception e) {
  69. e.printStackTrace();
  70. this.logger.error("DefaultException - uri: {} error: {} exception: {}", req.getRequestURI(), e.getMessage(), e.getClass().getName());
  71. if (interceptEnable) {
  72. errorService.addThrowable(e, req);
  73. }
  74. return buildFailResponse(4004, "系统错误,请联系技术人员!");
  75. }
  76. @ExceptionHandler(value = CheckException.class)
  77. public BaseResponse checkErrorHandler(CheckException e, HttpServletRequest req) {
  78. e.printStackTrace();
  79. this.logger.error("DefaultException - uri: {} error: {} exception: {}", req.getRequestURI(), e.getMessage(), e.getClass().getName());
  80. if (interceptEnable) {
  81. errorService.addThrowable(e, req);
  82. }
  83. return buildFailResponse(4001, e.getMessage());
  84. }
  85. @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
  86. public BaseResponse HttpRequestMethodNotSupportedExceptionHandler(CheckException e, HttpServletRequest req) {
  87. e.printStackTrace();
  88. if (interceptEnable) {
  89. this.logger.error("HttpRequestMethodNotSupportedException - uri: {} error: {}", req.getRequestURI(), e.getMessage(), e);
  90. errorService.addThrowable(e, req);
  91. }
  92. return buildFailResponse(4005, e.getMessage());
  93. }
  94. }