修复ContextInfoBuilderAspect 捕获异常时返回CommonResponse.error(java.lang.String)可能造成的类型不一致问题

This commit is contained in:
xiajiafu 2022-06-15 11:01:58 +08:00
parent 799b43c08b
commit 2dc5f1ec3f

View File

@ -1,20 +1,5 @@
package cn.axzo.framework.auth.service;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.axzo.framework.auth.AuthException;
import cn.axzo.framework.auth.annotation.PreBuildContext;
import cn.axzo.framework.auth.constants.AuthConstants;
@ -28,7 +13,6 @@ import cn.axzo.framework.auth.domain.UserInfo;
import cn.axzo.framework.auth.domain.UserInfoMap;
import cn.axzo.framework.auth.enums.EnvEnum;
import cn.azxo.framework.common.logger.logback.PodNamespacePropertyDefiner;
import cn.azxo.framework.common.model.CommonResponse;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
@ -39,6 +23,19 @@ import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
@Slf4j
@Aspect
@ -73,7 +70,9 @@ public class ContextInfoBuilderAspect {
contextInfo = fillContextInfoByRequest(httpRequest);
} catch (Exception e) {
//一般情况是AuthException
return CommonResponse.error(e.getMessage());
// return CommonResponse.error(e.getMessage());
// fix by xiajiafu. 因为切入的方法返回值不一定是CommonResponse直接返回 CommonResponse.error()会报错
throw new AuthException(e.getMessage(), e);
}
try {
@ -82,7 +81,9 @@ public class ContextInfoBuilderAspect {
Object[] args = parseContextInfoAndReturnArgs(httpRequest, pjp, contextInfo);
return pjp.proceed(args);
} catch (Exception e) {
return CommonResponse.error(e.getMessage());
// return CommonResponse.error(e.getMessage());
// fix by xiajiafu. 因为切入的方法返回值不一定是CommonResponse直接返回 CommonResponse.error()会报错
throw new AuthException(e.getMessage(), e);
} finally {
ContextInfoHolder.clear();
}