blob: abcd0556a8f696a3ab0ee10152b32184e93dacc9 [file] [log] [blame]
Jinf50fba62025-06-09 22:47:24 +08001package com.example.myproject.config;
2
3import cn.dev33.satoken.exception.NotLoginException;
4import cn.dev33.satoken.exception.NotPermissionException;
5import cn.dev33.satoken.exception.NotRoleException;
6import com.example.myproject.common.base.Result;
7import com.example.myproject.exception.BusinessException;
8import org.springframework.web.bind.annotation.ControllerAdvice;
9import org.springframework.web.bind.annotation.ExceptionHandler;
10import org.springframework.web.bind.annotation.RestControllerAdvice;
11
12@RestControllerAdvice
13public class GlobalExceptionHandler {
14
15 @ExceptionHandler(BusinessException.class)
16 public Result handleException(BusinessException e) {
17 // 这里可以根据不同的异常类型返回不同的结果
18 return Result.error(e.getMessage());
19 }
20
21 @ExceptionHandler(value = {NotLoginException.class})
22 public Result unLoginHandler(){
23 return Result.error(401, "您还未登录");
24 }
25 @ExceptionHandler(value = {NotRoleException.class, NotPermissionException.class})
26 public Result handlerException() {
27 return Result.error(403, "您没有权限");
28 }
29}