GlobalExceptionAdvice.java 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package com.webchat.pay.advice;
  2. import com.webchat.common.bean.APIResponseBean;
  3. import com.webchat.common.bean.APIResponseBeanUtil;
  4. import com.webchat.common.enums.APIErrorCommonEnum;
  5. import com.webchat.common.exception.AuthException;
  6. import com.webchat.common.exception.BusinessException;
  7. import com.webchat.common.exception.NotFoundException;
  8. import com.webchat.pay.config.exception.PaymentAccessAuthException;
  9. import jakarta.servlet.http.HttpServletRequest;
  10. import lombok.extern.slf4j.Slf4j;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.http.converter.HttpMessageConversionException;
  14. import org.springframework.web.HttpRequestMethodNotSupportedException;
  15. import org.springframework.web.bind.MethodArgumentNotValidException;
  16. import org.springframework.web.bind.annotation.ControllerAdvice;
  17. import org.springframework.web.bind.annotation.ExceptionHandler;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. /***
  20. * 统一异常处理器
  21. * @Author 程序员七七 ==> http://www.coderutil.com网站作者
  22. */
  23. @Slf4j
  24. @ControllerAdvice
  25. public class GlobalExceptionAdvice {
  26. @ExceptionHandler(Exception.class)
  27. @ResponseBody
  28. public ResponseEntity<APIResponseBean> exceptionHandler(HttpServletRequest request, Exception e) {
  29. log.error("异常详情信息:", e);
  30. if (e instanceof PaymentAccessAuthException) {
  31. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((PaymentAccessAuthException) e).getCode(), e.getMessage());
  32. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  33. } else if (e instanceof AuthException) {
  34. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((AuthException) e).getCode(), e.getMessage());
  35. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  36. }
  37. if (e instanceof IllegalArgumentException) {
  38. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(500, e.getMessage());
  39. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  40. } else if (e instanceof BusinessException) {
  41. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((BusinessException) e).getCode(), e.getMessage());
  42. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  43. } else if (e instanceof NotFoundException) {
  44. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(((NotFoundException) e).getCode(), e.getMessage());
  45. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  46. } else if (e instanceof HttpRequestMethodNotSupportedException) {
  47. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(500, e.getMessage());
  48. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  49. } else if (e instanceof MethodArgumentNotValidException) {
  50. /***
  51. * 处理@Validated参数校验失败异常
  52. */
  53. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(APIErrorCommonEnum.VALID_EXCEPTION.getCode(),
  54. "参数校验异常");
  55. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  56. } else if (e instanceof HttpMessageConversionException) {
  57. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(APIErrorCommonEnum.VALID_EXCEPTION.getCode(),
  58. "参数转换异常");
  59. return new ResponseEntity(apiResponseBean, HttpStatus.OK);
  60. }
  61. APIResponseBean apiResponseBean = APIResponseBeanUtil.error(500, "服务端繁忙");
  62. return new ResponseEntity(apiResponseBean, HttpStatus.INTERNAL_SERVER_ERROR);
  63. }
  64. }