chenlu
2024-06-07 2f640b1038fa331954f78ed1f4317212cf5bb34d
north-glass-erp/src/main/java/com/example/erp/exception/GlobalExceptionHandle.java
New file
@@ -0,0 +1,39 @@
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");
    }
}