Jin | f50fba6 | 2025-06-09 22:47:24 +0800 | [diff] [blame^] | 1 | package com.example.myproject.config; |
| 2 | |
| 3 | import cn.dev33.satoken.exception.NotLoginException; |
| 4 | import cn.dev33.satoken.exception.NotPermissionException; |
| 5 | import cn.dev33.satoken.exception.NotRoleException; |
| 6 | import com.example.myproject.common.base.Result; |
| 7 | import com.example.myproject.exception.BusinessException; |
| 8 | import org.springframework.web.bind.annotation.ControllerAdvice; |
| 9 | import org.springframework.web.bind.annotation.ExceptionHandler; |
| 10 | import org.springframework.web.bind.annotation.RestControllerAdvice; |
| 11 | |
| 12 | @RestControllerAdvice |
| 13 | public 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 | } |