问题: 在自定义切面类around方法内执行point.proceed()时捕获的异常被转化为了自定义异常,并且没有具体的异常信息
原因:封装的切面类和自定义切面类调用了同一个方法并且执行point.proceed()时也进行了异常处理,导致空指针异常被封装为自定义异常,从而丢失了封装信息
如下为封装的切面类的异常处理:
} catch (Throwable ex) {
ex = JdEx.getHandleException(ex);
if (ex.getClass().equals(JdEx.class) == false) {
var duration = Duration.between(startDate, LocalDateTime.now());
addApiLog(request, method, args, null, duration.toMillis(), ex);
var msg = formatThrowableMessage(ex);
ex = new JdEx(msg);
}
throw ex;
}
private String formatThrowableMessage(Throwable ex) {
var msg = ex.getMessage();
try {
//Dubbo的服务者未注册错误格式化
if (msg.startsWith("No provider available from registry")) {
var methodClassName = Linq.of(msg.substring(msg.indexOf("com.jdjqkj.")).split(" ")[0].split(":")[0].split("\\.")).last();
msg = "错误消息:" + methodClassName + "微服务注册失败";
}
} catch (Exception exx) {
}
return msg;
}
为不修改封装方法异常处理逻辑,对封装的切面类增加@Order注解:
@Order(100)
设置优先级为100,自定义切面类顺序设置为10,目的为了在封装切面类前面执行,问题出现:
优先级为10的的自定义切面类在执行point.proceed()后并没有进入catch块而是走了封装切面类point.proceed()方法,后又直接进入了上述代码封装切面类的异常catch块从而导致异常变成了自定义异常。
如下为自定义切面类:
/**
* 系统日志切面
*/
@Aspect // 使用@Aspect注解声明一个切面
@Component
@Order(10)//设置相对于jdlibrary封装的切面类(Order:100)的优先级 值越小越先调用
public class SysLogAspect {
/**
* 通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method
* 切点表达式: execution(...)
*/
@Pointcut("execution(* com.*.*.controller.*.*(..))")
public void logPointCut() {
}
/**
* 环绕通知 @Around , 当然也可以使用 @Before (前置通知) @After (后置通知)
*
* @param point
* @return
* @throws Throwable
*/
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
MethodSignature signature = (MethodSignature) point.getSignature();
var method = signature.getMethod();
Class clazz = Class.forName(method.getDeclaringClass().getName());
var classOperation = (Api) clazz.getAnnotation(Api.class); //类的swagger注解
var methodOperation = (OperateModule) method.getAnnotation(OperateModule.class); //方法所属功能模块的自定义注解
String moduleName = "";//功能模块名称
if (methodOperation != null) {//优先取方法的自定义注解
moduleName = methodOperation.name();
}
if (JdStringUtil.isEmpty(moduleName) && classOperation != null) {
String[] tags = classOperation.tags();
if (tags.length > 0) {
moduleName = tags[0];//功能模块名称
}
}
request.setAttribute("moduleName", moduleName);
var time = System.currentTimeMillis();
try {
Object result = point.proceed();
SysLogUtil.addApiLog(request, result, System.currentTimeMillis() - time);
return result;
} catch (Throwable ex) {
if (ex instanceof JdEx == false) {//不是自定义异常则记录日志
SysLogUtil.addApiErrorLog(request, ex);
}
throw ex;
}
}
}
经debug得出结论,切面类方法调用顺序:先执行的最后结束
order值小的point.proceed()先执行;其次执行order值大的point.proceed();然后进入order值大的catch代码块;最后进入order值小的catch代码块。
附转自:https://blog.csdn.net/Aeve_imp/article/details/93098524博客的一张图