GlobalExceptionHandler.java 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. package com.example.onlinejudge.exception;
  2. import cn.dev33.satoken.exception.NotLoginException;
  3. import cn.dev33.satoken.util.SaResult;
  4. import com.example.onlinejudge.model.entity.result.Result;
  5. import com.example.onlinejudge.model.entity.result.ResultCode;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.RestControllerAdvice;
  9. /**
  10. * 全局拦截异常
  11. */
  12. @RestControllerAdvice
  13. @Slf4j
  14. public class GlobalExceptionHandler {
  15. /**
  16. * 拦截未登录的用户进行非法权限操作
  17. * @param e NotLoginException
  18. * @return 返回未登录错误
  19. */
  20. @ExceptionHandler(NotLoginException.class)
  21. public Result handlerException(NotLoginException e) {
  22. e.printStackTrace();
  23. return Result.error(ResultCode.USER_NOTLOGGED_IN);
  24. }
  25. @ExceptionHandler(BusinessException.class)
  26. public Result<?> businessExceptionHandler(BusinessException e) {
  27. log.error("BusinessException", e);
  28. return Result.error(e.getCode(), e.getMessage());
  29. }
  30. }