CustomExceptionHandler.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.fdkankan.openApi.exception;
  2. import cn.dev33.satoken.exception.SaTokenException;
  3. import cn.hutool.core.util.NumberUtil;
  4. import com.fdkankan.common.constant.ErrorCode;
  5. import com.fdkankan.common.exception.BusinessException;
  6. import com.fdkankan.web.response.ResultData;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.web.bind.annotation.ExceptionHandler;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import org.springframework.web.bind.annotation.RestControllerAdvice;
  11. /**
  12. * 全局异常处理器
  13. */
  14. @RestControllerAdvice
  15. @Slf4j
  16. public class CustomExceptionHandler {
  17. /**
  18. * 处理未知异常
  19. */
  20. @ResponseBody
  21. @ExceptionHandler(value = Exception.class)
  22. public ResultData exceptionHandler(Exception e) throws Exception {
  23. log.error("服务错误:", e);
  24. return ResultData.error( 500, "系统错误");
  25. }
  26. @ExceptionHandler(SaTokenException.class)
  27. public ResultData handlerSaTokenException(SaTokenException e) {
  28. if( NumberUtil.equals(e.getCode() , 11012)|| NumberUtil.equals(e.getCode() , 11011)) {
  29. return ResultData.error(ErrorCode.USER_NOT_LOGIN);
  30. }
  31. return ResultData.error(e.getCode(),e.getMessage());
  32. }
  33. /**
  34. * 处理业务异常
  35. */
  36. @ResponseBody
  37. @ExceptionHandler(value = BusinessException.class)
  38. public ResultData businessExceptionHandler(BusinessException e) {
  39. log.warn("业务异常code:{},message:{}", e.getCode(), e.getMessage());
  40. return ResultData.error(e.getCode(), e.getMessage());
  41. }
  42. }