GlobalExceptionHandler.java 1.6 KB

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