廖井涛
2024-04-28 ed2823ccf96675ab29414a588bba85bb8852ef9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.erp.exception;
 
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.util.SaResult;
import com.example.erp.common.Constants;
import com.example.erp.common.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.Arrays;
 
@ControllerAdvice
public class GlobalExceptionHandle {
    /**
     * 如果是serviceExcaption,则调用该方法
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public Result handle(ServiceException se){
        return Result.error(se.getCode(),se.getMessage());
    }
 
    @ExceptionHandler(NotPermissionException.class)
    @ResponseBody
    public Result handlerException(NotPermissionException e) {
        return Result.error(Constants.Code_401,e.getPermission());
    }
 
    // 拦截:未登录异常
    @ExceptionHandler(NotLoginException.class)
    @ResponseBody
    public Result handlerException(NotLoginException e) {
        // 返回给前端
        return Result.error(Constants.Code_402,"Please login user");
    }
}