GlobalExceptionHandler.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.fdkankan.web.exception;
  2. import cn.hutool.core.exceptions.ExceptionUtil;
  3. import com.fdkankan.common.constant.ErrorCode;
  4. import com.fdkankan.common.constant.ServerCode;
  5. import com.fdkankan.common.exception.BusinessException;
  6. import com.fdkankan.web.response.ResultData;
  7. import java.util.List;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.http.converter.HttpMessageNotReadableException;
  12. import org.springframework.validation.BindException;
  13. import org.springframework.validation.BindingResult;
  14. import org.springframework.validation.FieldError;
  15. import org.springframework.validation.ObjectError;
  16. import org.springframework.web.bind.MethodArgumentNotValidException;
  17. import org.springframework.web.bind.MissingServletRequestParameterException;
  18. import org.springframework.web.bind.annotation.ExceptionHandler;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.bind.annotation.RestControllerAdvice;
  21. import org.springframework.web.multipart.MultipartException;
  22. /**
  23. * 全局异常处理器
  24. */
  25. @RestControllerAdvice
  26. @Slf4j
  27. public class GlobalExceptionHandler {
  28. /**
  29. * 处理未知异常
  30. */
  31. @ResponseBody
  32. @ExceptionHandler(value = Exception.class)
  33. public ResultData exceptionHandler(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Exception e) throws Exception {
  34. log.error("服务错误:", e);
  35. //增加sentinel异常次数
  36. // Tracer.trace(e);
  37. return ResultData.error(ServerCode.SYSTEM_ERROR.code(), ExceptionUtil.stacktraceToString(e, 3000));
  38. }
  39. /**
  40. * 限流熔断异常处理
  41. * 如果在这里捕获的话,就需要在接口中加入@SentinelResour注解
  42. */
  43. // @ResponseBody
  44. // @ExceptionHandler({FlowException.class, DegradeException.class})
  45. // public ResultData flowExceptionHandler(HttpServletRequest httpServletRequest, Exception e) {
  46. // String requestURI = httpServletRequest.getRequestURI();
  47. // String resource = null;
  48. // if(e instanceof FlowException){
  49. // resource = ((FlowException) e).getRule().getResource();
  50. // }
  51. // if(e instanceof DegradeException){
  52. // resource = ((DegradeException) e).getRule().getResource();
  53. // }
  54. //
  55. // log.error("请求限流:requestURI{},resource:{}", requestURI, resource);
  56. // return ResultData.error(ServerCode.SYSTEM_ERROR.code(), "系统繁忙,请稍后重试");
  57. // }
  58. /**
  59. * <p>
  60. form表达提交,缺少参数异常处理
  61. * </p>
  62. * @author dengsixing
  63. * @date 2022/4/14
  64. * @param e
  65. * @return com.fdkankan.common.response.ResultData
  66. **/
  67. @ResponseBody
  68. @ExceptionHandler(value = MissingServletRequestParameterException.class)
  69. public ResultData missingServletRequestParameterException(
  70. MissingServletRequestParameterException e){
  71. return ResultData.error(
  72. ServerCode.PARAM_REQUIRED.code(),
  73. ServerCode.PARAM_REQUIRED.formatMessage(e.getParameterName()));
  74. }
  75. /**
  76. * <p>
  77. form表达提交,文件为空异常处理
  78. * </p>
  79. * @author dengsixing
  80. * @date 2022/4/14
  81. * @param e
  82. * @return com.fdkankan.common.response.ResultData
  83. **/
  84. @ResponseBody
  85. @ExceptionHandler(value = MultipartException.class)
  86. public ResultData multipartException(MultipartException e){
  87. return ResultData.error(
  88. ServerCode.PARAM_REQUIRED.code(),
  89. ServerCode.PARAM_REQUIRED.formatMessage("文件"));
  90. }
  91. @ResponseBody
  92. @ExceptionHandler(value = HttpMessageNotReadableException.class)
  93. public ResultData httpMessageNotReadableException(HttpMessageNotReadableException e){
  94. return ResultData.error(ErrorCode.FAILURE_CODE_3001);
  95. }
  96. /**
  97. * 参数校验异常拦截处理
  98. */
  99. @ResponseBody
  100. @ExceptionHandler({BindException.class,MethodArgumentNotValidException.class})
  101. public ResultData validationBodyException(Exception e) {
  102. BindingResult result = null;
  103. if(e instanceof MethodArgumentNotValidException){
  104. result = ((MethodArgumentNotValidException) e).getBindingResult();
  105. }
  106. if(e instanceof BindException){
  107. result = ((BindException) e).getBindingResult();
  108. }
  109. String message = "";
  110. if (result.hasErrors()) {
  111. List<ObjectError> errors = result.getAllErrors();
  112. if (errors != null) {
  113. errors.forEach(p -> {
  114. FieldError fieldError = (FieldError) p;
  115. log.error("参数校验错误:类:{},字段:{},错误信息:{}",fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());
  116. });
  117. if (errors.size() > 0) {
  118. FieldError fieldError = (FieldError) errors.get(0);
  119. message = fieldError.getDefaultMessage();
  120. }
  121. }
  122. }
  123. return ResultData.error(ErrorCode.PARAM_REQUIRED.code(), message);
  124. }
  125. /**
  126. * 处理业务异常
  127. */
  128. @ResponseBody
  129. @ExceptionHandler(value = BusinessException.class)
  130. public ResultData businessExceptionHandler(HttpServletRequest httpServletRequest, BusinessException e) {
  131. log.info("业务异常code:{},message:{}", e.getCode(), e.getMessage());
  132. return ResultData.error(e.getCode(), e.getMessage());
  133. }
  134. }