GlobalExceptionHandler.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.futu.course.common.exception;
  2. import com.futu.course.common.constant.BestConstants;
  3. import com.futu.course.common.entity.R;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.jdbc.BadSqlGrammarException;
  7. import org.springframework.validation.BindException;
  8. import org.springframework.web.bind.MethodArgumentNotValidException;
  9. import org.springframework.web.bind.annotation.ExceptionHandler;
  10. import org.springframework.web.bind.annotation.RestControllerAdvice;
  11. import javax.servlet.http.HttpServletRequest;
  12. import java.sql.SQLException;
  13. /**
  14. * 全局异常处理器
  15. */
  16. @RestControllerAdvice
  17. public class GlobalExceptionHandler {
  18. private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  19. /**
  20. * 拦截未知的运行时异常
  21. */
  22. @ExceptionHandler(RuntimeException.class)
  23. public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
  24. String requestURI = request.getRequestURI();
  25. log.error("请求地址'{}',发生未知异常.", requestURI, e);
  26. return R.failed(e.getMessage() == null ? "发生未知错误,请联系管理员!" : e.getMessage());
  27. }
  28. /**
  29. * 处理自定义异常
  30. */
  31. @ExceptionHandler(RRException.class)
  32. public R handleRRException(RRException e, HttpServletRequest request) {
  33. String requestURI = request.getRequestURI();
  34. // log.error("请求地址'{}',发生系统异常.", requestURI, e);
  35. log.info("自定义异常");
  36. return R.failed(e.getCode(), e.getMsg());
  37. }
  38. /**
  39. * 系统异常
  40. */
  41. @ExceptionHandler(Exception.class)
  42. public R handleException(Exception e, HttpServletRequest request) {
  43. String requestURI = request.getRequestURI();
  44. log.error("请求地址'{}',发生系统异常.", requestURI, e);
  45. return R.failed("系统错误,请联系管理员!");
  46. }
  47. /**
  48. * 系统异常
  49. */
  50. @ExceptionHandler(BadSqlGrammarException.class)
  51. public R handleBadSQLException(BadSqlGrammarException e, HttpServletRequest request) {
  52. String requestURI = request.getRequestURI();
  53. log.error("请求地址'{}',发生系统异常.", requestURI, e);
  54. return R.failed("系统错误,请联系管理员!");
  55. }
  56. /**
  57. * 系统异常
  58. */
  59. @ExceptionHandler(SQLException.class)
  60. public R handleSQLException(SQLException e, HttpServletRequest request) {
  61. String requestURI = request.getRequestURI();
  62. log.error("请求地址'{}',发生系统异常.", requestURI, e);
  63. return R.failed("系统错误,请联系管理员!");
  64. }
  65. /**
  66. * 自定义验证异常
  67. */
  68. @ExceptionHandler(BindException.class)
  69. public R handleBindException(BindException e) {
  70. // log.error(e.getMessage(), e);
  71. String message = e.getAllErrors().get(0).getDefaultMessage();
  72. return R.failed(message);
  73. }
  74. /**
  75. * 自定义验证异常
  76. */
  77. @ExceptionHandler(UnBindPhoneException.class)
  78. public R handleUnBindPhoneException(UnBindPhoneException e) {
  79. // log.error(e.getMessage(), e);
  80. return R.failed(BestConstants.USER_NOT_PHONE_CODE,BestConstants.USER_NOT_PHONE_MSG);
  81. }
  82. /**
  83. * 自定义验证异常
  84. */
  85. @ExceptionHandler(MethodArgumentNotValidException.class)
  86. public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  87. // log.error(e.getMessage(), e);
  88. log.info("自定义验证异常");
  89. String message = e.getBindingResult().getFieldError().getDefaultMessage();
  90. return R.failed(message);
  91. }
  92. }