package com.cslg.ppa.common.exception; import cn.dev33.satoken.exception.NotLoginException; import com.cslg.ppa.common.core.auth.Response; import com.cslg.ppa.common.core.auth.ResponseEnum; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; /** * 全局异常处理 */ @Slf4j @RestControllerAdvice // 可指定包前缀,比如:(basePackages = "com.pj.admin") public class GlobalException { // 全局异常拦截(拦截项目中的所有异常) @ExceptionHandler public String handlerException(Exception e, HttpServletRequest request, HttpServletResponse response) throws Exception { // 打印堆栈,以供调试 e.printStackTrace(); if (e instanceof NotLoginException) { return Response.error(ResponseEnum.UNAUTHORIZED); } else { return Response.error(e.getMessage()); } } //小世异常 @ExceptionHandler public String handlerXiaoShiException(XiaoShiException e) { log.info("全局异常处理机制捕获到XiaoShiException,异常信息提示为:{}", e.getErrorCode() + "--" + e.getMessage()); if (StringUtils.isNotEmpty(e.getErrorCode())) { return Response.error(Integer.parseInt(e.getErrorCode()), e.getErrorMessage()); } else { return Response.error(e.getMessage()); } } }