package {{ package.Common }}.config; import {{ package.Common }}.vo.Result; //--- 固定引入 ---// import cn.dev33.satoken.exception.*; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; //--- 固定引入 ---// /** * 全局异常处理 * @Author: {{author}} * @Date: {{date}} * @Wechat: {{ wechat }} */ @RestControllerAdvice public class GlobalException { // 拦截:未登录异常 @ExceptionHandler(NotLoginException.class) public Result handlerException(NotLoginException e) { // 打印堆栈,以供调试 e.printStackTrace(); // 返回给前端 return Result.error(1000,e.getMessage()); } // 拦截:缺少权限异常 @ExceptionHandler(NotPermissionException.class) public Result handlerException(NotPermissionException e) { e.printStackTrace(); return Result.error("缺少权限:" + e.getPermission()); } // 拦截:缺少角色异常 @ExceptionHandler(NotRoleException.class) public Result handlerException(NotRoleException e) { e.printStackTrace(); return Result.error("缺少角色:" + e.getRole()); } // 拦截:二级认证校验失败异常 @ExceptionHandler(NotSafeException.class) public Result handlerException(NotSafeException e) { e.printStackTrace(); return Result.error("二级认证校验失败:" + e.getService()); } // 拦截:服务封禁异常 @ExceptionHandler(DisableServiceException.class) public Result handlerException(DisableServiceException e) { e.printStackTrace(); return Result.error("当前账号 " + e.getService() + " 服务已被封禁 (level=" + e.getLevel() + "):" + e.getDisableTime() + "秒后解封"); } // 拦截:Http Basic 校验失败异常 @ExceptionHandler(NotBasicAuthException.class) public Result handlerException(NotBasicAuthException e) { e.printStackTrace(); return Result.error(1003,e.getMessage()); } // 拦截:其它所有异常 @ExceptionHandler(Exception.class) public Result handlerException(Exception e) { e.printStackTrace(); return Result.error(1002,e.getMessage()); } }