CommonExceptionHandler.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.persagy.dmp.common.handler;
  2. import cn.hutool.core.exceptions.ExceptionUtil;
  3. import com.persagy.dmp.common.constant.ResponseCode;
  4. import com.persagy.dmp.common.exception.BusinessException;
  5. import com.persagy.dmp.common.model.response.CommonResult;
  6. import com.persagy.dmp.common.utils.ResultHelper;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.http.HttpStatus;
  9. import org.springframework.http.converter.HttpMessageNotReadableException;
  10. import org.springframework.validation.BindException;
  11. import org.springframework.web.bind.MethodArgumentNotValidException;
  12. import org.springframework.web.bind.annotation.ExceptionHandler;
  13. import org.springframework.web.bind.annotation.ResponseStatus;
  14. import org.springframework.web.bind.annotation.RestControllerAdvice;
  15. import org.springframework.web.servlet.NoHandlerFoundException;
  16. import javax.validation.ValidationException;
  17. import java.sql.SQLException;
  18. /**
  19. * @description: 通用异常处理类
  20. * @author: lixing
  21. * @since: 2021/3/5 5:10 下午
  22. **/
  23. @RestControllerAdvice
  24. @Slf4j
  25. public class CommonExceptionHandler {
  26. /**
  27. * 空指针异常
  28. */
  29. @ExceptionHandler(NullPointerException.class)
  30. public CommonResult handleBusinessRuntimeException(NullPointerException e) {
  31. log.error(e.getMessage(), e);
  32. return ResultHelper.failure(ResponseCode.B0001.getCode(), ExceptionUtil.getRootCauseMessage(e));
  33. }
  34. /**
  35. * 自定义业务异常
  36. */
  37. @ExceptionHandler(BusinessException.class)
  38. public CommonResult handleBusinessRuntimeException(BusinessException e) {
  39. log.error(e.getMessage(), e);
  40. return ResultHelper.failure(e.getErrorCode(), e.getErrorDesc());
  41. }
  42. /**
  43. * SQL异常
  44. * @param e
  45. * @return
  46. */
  47. @ExceptionHandler(SQLException.class)
  48. public CommonResult handleSqlException(SQLException e) {
  49. log.error(e.getMessage(), e);
  50. return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getMessage());
  51. }
  52. /**
  53. * 入参校验异常
  54. */
  55. @ExceptionHandler(MethodArgumentNotValidException.class)
  56. public CommonResult handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  57. log.error(e.getMessage(), e);
  58. return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
  59. }
  60. /**
  61. * 入参校验异常
  62. */
  63. @ExceptionHandler(BindException.class)
  64. public CommonResult handleBindException(BindException e) {
  65. log.error(e.getMessage(), e);
  66. return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
  67. }
  68. /**
  69. * 参数校验异常,message 为手动抛出时定义的具体异常信息
  70. */
  71. @ExceptionHandler(ValidationException.class)
  72. public CommonResult handleValidationException(ValidationException e) {
  73. log.error(e.getMessage(), e);
  74. return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getCause().getMessage());
  75. }
  76. /**
  77. * 参数校验异常,message 为手动抛出时定义的具体异常信息
  78. */
  79. @ExceptionHandler(IllegalArgumentException.class)
  80. public CommonResult handleIllegalArgumentException(IllegalArgumentException e) {
  81. log.error(e.getMessage(), e);
  82. return ResultHelper.failure(ResponseCode.A0400.getCode(), e.getMessage());
  83. }
  84. /**
  85. * URI请求链接没有映射
  86. */
  87. @ResponseStatus(HttpStatus.NOT_FOUND)
  88. @ExceptionHandler(NoHandlerFoundException.class)
  89. public CommonResult handleNoHandlerFoundException(NoHandlerFoundException e) {
  90. log.error(e.getMessage(), e);
  91. return ResultHelper.failure(ResponseCode.A0422.getCode(), e.getMessage());
  92. }
  93. /**
  94. * body体未找到
  95. */
  96. @ResponseStatus(HttpStatus.BAD_REQUEST)
  97. @ExceptionHandler(HttpMessageNotReadableException.class)
  98. public CommonResult handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
  99. log.error(e.getMessage(), e);
  100. return ResultHelper.failure(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
  101. }
  102. /**
  103. * 系统默认繁忙
  104. */
  105. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  106. @ExceptionHandler(Exception.class)
  107. public CommonResult handleException(Exception e) {
  108. log.error(e.getMessage(), e);
  109. // 20210819 返回实际的异常信息
  110. return ResultHelper.failure(ResponseCode.Z9999.getCode(), ExceptionUtil.getRootCauseMessage(e));
  111. }
  112. }