MyExceptionHandler.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.chuanghai.attendance.common.exception;
  2. import com.alibaba.excel.exception.ExcelAnalysisException;
  3. import com.chuanghai.attendance.common.utils.CommonResult;
  4. import org.springframework.dao.DuplicateKeyException;
  5. import org.springframework.http.converter.HttpMessageNotReadableException;
  6. import org.springframework.util.StringUtils;
  7. import org.springframework.web.HttpRequestMethodNotSupportedException;
  8. import org.springframework.web.bind.MethodArgumentNotValidException;
  9. import org.springframework.web.bind.MissingRequestHeaderException;
  10. import org.springframework.web.bind.MissingServletRequestParameterException;
  11. import org.springframework.web.bind.annotation.ExceptionHandler;
  12. import org.springframework.web.bind.annotation.RestControllerAdvice;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. /**
  16. * @Author: codingliang
  17. * @Description: 统一异常处理
  18. * @Date: 2021-01-06 10:07
  19. * @Version: V1.0
  20. **/
  21. @RestControllerAdvice
  22. public class MyExceptionHandler {
  23. /**
  24. * 处理参数校验异常
  25. * @param e
  26. * @return
  27. */
  28. @ExceptionHandler(MethodArgumentNotValidException.class)
  29. public CommonResult handleValidException(MethodArgumentNotValidException e) {
  30. Map<String, String> map = new HashMap<>();
  31. e.getBindingResult().getFieldErrors().forEach(item -> {
  32. String errMessage = item.getDefaultMessage();
  33. String errField = item.getField();
  34. map.put(errField, errMessage);
  35. });
  36. return CommonResult.fail(Integer.toString(BizCodeEnume.VAILD_EXCEPTION.getCode()), BizCodeEnume.VAILD_EXCEPTION.getMsg()).setResult(map);
  37. }
  38. /**
  39. * 数据库索引重复
  40. * @return
  41. */
  42. @ExceptionHandler(DuplicateKeyException.class)
  43. public CommonResult handleSQLIntegrityConstraintViolationException() {
  44. return CommonResult.fail(Integer.toString(BizCodeEnume.DATA_IS_EXIST.getCode()), BizCodeEnume.DATA_IS_EXIST.getMsg());
  45. }
  46. /**
  47. * 请求方法不支持
  48. * @return
  49. */
  50. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  51. public CommonResult handleRequestMethodNotSupported() {
  52. return CommonResult.fail(Integer.toString(BizCodeEnume.METHOD_NOT_SUPPORT.getCode()), BizCodeEnume.METHOD_NOT_SUPPORT.getMsg());
  53. }
  54. /**
  55. * 缺少请求头
  56. * @return
  57. */
  58. @ExceptionHandler(MissingRequestHeaderException.class)
  59. public CommonResult handleRequestHeaderMissing() {
  60. return CommonResult.fail(Integer.toString(BizCodeEnume.REQUEST_HEADER_MISSING.getCode()), BizCodeEnume.REQUEST_HEADER_MISSING.getMsg());
  61. }
  62. /**
  63. * 参数缺失
  64. * @return
  65. */
  66. @ExceptionHandler(MissingServletRequestParameterException.class)
  67. public CommonResult handleMissingServletRequestParameterException() {
  68. return CommonResult.fail(Integer.toString(BizCodeEnume.PARAMETER_ERROR.getCode()), BizCodeEnume.PARAMETER_ERROR.getMsg());
  69. }
  70. /**
  71. * body为空
  72. * @return
  73. */
  74. @ExceptionHandler(HttpMessageNotReadableException.class)
  75. public CommonResult handleMessageNotReadableException() {
  76. return CommonResult.fail(Integer.toString(BizCodeEnume.BODY_IS_EMPTY.getCode()), BizCodeEnume.BODY_IS_EMPTY.getMsg());
  77. }
  78. /**
  79. * easyExcel 文件读取失败
  80. * @return
  81. */
  82. @ExceptionHandler(ExcelAnalysisException.class)
  83. public CommonResult handleExcelAnalysisException(ExcelAnalysisException e) {
  84. String errorMessage = e.getMessage();
  85. System.out.println(errorMessage);
  86. String errMsg = "文件导入失败";
  87. if (StringUtils.hasText(errorMessage) && errorMessage.contains("doesn't have a default value")) {
  88. errMsg = "文件导入失败,请检查模板header是否正确";
  89. } else if (StringUtils.hasText(errorMessage) && errorMessage.contains("Duplicate entity")) {
  90. errMsg = "文件导入失败,含有重复数据";
  91. }else if (StringUtils.hasText(errorMessage) && errorMessage.contains("数据已存在") ){
  92. errMsg = "文件导入失败,含有重复数据";
  93. }
  94. return CommonResult.fail(Integer.toString(BizCodeEnume.FILE_IMPORT_ERROR.getCode()), errMsg);
  95. }
  96. /**
  97. * 自定义异常
  98. * @param e
  99. * @return
  100. */
  101. @ExceptionHandler(RRException.class)
  102. public CommonResult handleRRException(RRException e) {
  103. return CommonResult.fail(Integer.toString(e.getCode()), e.getMsg());
  104. }
  105. /**
  106. * 未知异常
  107. * @param throwable
  108. * @return
  109. */
  110. @ExceptionHandler(Throwable.class)
  111. public CommonResult handleException(Throwable throwable) {
  112. throwable.printStackTrace();
  113. return CommonResult.fail(Integer.toString(BizCodeEnume.UNKNOW_EXCEPTION.getCode()), BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
  114. }
  115. }