package com.example.onlinejudge.exception; import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.util.SaResult; import com.example.onlinejudge.model.entity.result.Result; import com.example.onlinejudge.model.entity.result.ResultCode; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * 全局拦截异常 */ @RestControllerAdvice @Slf4j public class GlobalExceptionHandler { /** * 拦截未登录的用户进行非法权限操作 * @param e NotLoginException * @return 返回未登录错误 */ @ExceptionHandler(NotLoginException.class) public Result handlerException(NotLoginException e) { e.printStackTrace(); return Result.error(ResultCode.USER_NOTLOGGED_IN); } @ExceptionHandler(BusinessException.class) public Result businessExceptionHandler(BusinessException e) { log.error("BusinessException", e); return Result.error(e.getCode(), e.getMessage()); } }