GlobalExceptionHandler.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.poyee.common.exception;
  2. import com.poyee.base.dto.Result;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.validation.BindException;
  6. import org.springframework.validation.ObjectError;
  7. import org.springframework.web.HttpRequestMethodNotSupportedException;
  8. import org.springframework.web.bind.MethodArgumentNotValidException;
  9. import org.springframework.web.bind.annotation.ControllerAdvice;
  10. import org.springframework.web.bind.annotation.ExceptionHandler;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14. /**
  15. *
  16. */
  17. @ControllerAdvice
  18. @ResponseBody
  19. public class GlobalExceptionHandler extends RuntimeException {
  20. private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  21. /**
  22. * 权限校验失败 如果请求为ajax返回json,普通请求跳转页面
  23. */
  24. /*@ExceptionHandler(AuthorizationException.class)
  25. public Object handleAuthorizationException(HttpServletRequest request, AuthorizationException e)
  26. {
  27. log.error(e.getMessage(), e);
  28. if (ServletUtils.isAjaxRequest(request))
  29. {
  30. return AjaxResult.error(PermissionUtils.getMsg(e.getMessage()));
  31. }
  32. else
  33. {
  34. ModelAndView modelAndView = new ModelAndView();
  35. modelAndView.setViewName("error/unauth");
  36. return modelAndView;
  37. }
  38. }*/
  39. /**
  40. * 请求方式不支持
  41. */
  42. @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
  43. public Result handleException(HttpRequestMethodNotSupportedException e)
  44. {
  45. log.error("运行异常:{}", e);
  46. return Result.error("不支持' " + e.getMethod() + "'请求");
  47. }
  48. /**
  49. * 拦截未知的运行时异常
  50. */
  51. @ExceptionHandler(RuntimeException.class)
  52. public Object notFount(RuntimeException e)
  53. {
  54. log.error("运行时异常:{}", e);
  55. if(e.getCause() instanceof SQLException){
  56. return Result.error("sql运行异常,请联系管理员");
  57. }
  58. return Result.error("运行出错了:" + e.getMessage());
  59. }
  60. @ExceptionHandler(ServiceException.class)
  61. public Result serviceErr(ServiceException e) {
  62. log.error("运行时异常:{}", e);
  63. if(e.getCode() == 0){
  64. e.code = -1;
  65. }
  66. return Result.error(e.getCode(),e.getMessage());
  67. }
  68. /**
  69. * 系统异常
  70. */
  71. @ExceptionHandler(Exception.class)
  72. public Result handleException(Exception e)
  73. {
  74. log.error("运行异常:{}", e);
  75. return Result.error("服务器错误,请联系管理员");
  76. }
  77. /**
  78. * 业务异常
  79. */
  80. @ExceptionHandler(BaseException.class)
  81. public Object baseException(BaseException e)
  82. {
  83. log.error(e.getMessage(), e);
  84. String errMsg = e.getMessage();
  85. return Result.error(errMsg);
  86. }
  87. /**
  88. * 业务异常
  89. */
  90. @ExceptionHandler(BusinessException.class)
  91. public Object businessException( BusinessException e)
  92. {
  93. log.error(e.getMessage(), e);
  94. String errMsg = e.getMessage();
  95. Integer code = e.getCode();
  96. return Result.error(code,errMsg);
  97. }
  98. /**
  99. * 自定义验证异常
  100. */
  101. @ExceptionHandler(BindException.class)
  102. public Result validatedBindException(BindException e)
  103. {
  104. log.error(e.getMessage(), e);
  105. String message = e.getAllErrors().get(0).getDefaultMessage();
  106. return Result.error(message);
  107. }
  108. @ExceptionHandler(MethodArgumentNotValidException.class)
  109. public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  110. StringBuilder msg = new StringBuilder();
  111. List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
  112. for (ObjectError allError : allErrors) {
  113. msg.append(allError.getDefaultMessage()).append(",");
  114. }
  115. if (null != msg && msg.length()>1) {
  116. return Result.error(msg.substring(0,msg.length()-1));
  117. }
  118. return Result.error(msg.toString());
  119. }
  120. }