1.概述
1.1 背景简介
项目采用SSM框架,通信采用Dubbo框架,web层(comsumer层)定义了一个全局异常处理类,逻辑中判断如果捕获到的异常是自定义异常,
那么会把自定义异常中的code和message拿出来,通过json格式返给前端,这样前端在判断非正 常码时,直接打印message即可。
但是在一次bug解决过程当中,发现从service层抛到web层的自定义异常(BusinessException extends RuntimeException)
被识别成了其他异常,导致前端没有打印自定义异常中的message。而同一个类中,另一个方法也是抛出的相同异常,却可以打印,即全局异常
处理类将其捕获并走自定义异常的处理逻辑。WTF???
1.2 部分截图
1.2.1 全局异常处理类关于自定义异常逻辑代码
1.2.2 service层打印异常日志
1.2.3 web层打印异常日志
2.问题定位及解决
2.1源码分析
通过查阅相关资料,找到问题所在,是因为dubbo框架在异常往调用方抛出时,有一些判断逻辑处理,部分源码如下所示:
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
try {
Result result = invoker.invoke(invocation);
if (result.hasException() && GenericService.class != invoker.getInterface()) {
try {
Throwable exception = result.getException();
// checked异常(IOException等),直接抛出
if (! (exception instanceof RuntimeException) && (exception instanceof Exception)) {
return result;
}
// 在方法签名上有声明(throws BusinessException),直接抛出
// 注意查看最后抛到web层的那个方法签名处是否有声明
try {
Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes());
Class<?>[] exceptionClassses = method.getExceptionTypes();
for (Class<?> exceptionClass : exceptionClassses) {
if (exception.getClass().equals(exceptionClass)) {
return result;
}
}
} catch (NoSuchMethodException e) {
return result;
}
// 未在方法签名上定义的异常,在service层打印ERROR日志
// 这也就是上面截图中service层日志截图中打印的日志!!!
logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
+ ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception);
// 异常类和接口类在同一jar包里,直接抛出
String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface());
String exceptionFile = ReflectUtils.getCodeBase(exception.getClass());
if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)){
return result;
}
// 是JDK自带的异常,直接抛出
String className = exception.getClass().getName();
if (className.startsWith("java.") || className.startsWith("javax.")) {
return result;
}
// 是Dubbo本身的异常,直接抛出
if (exception instanceof RpcException) {
return result;
}
// 否则,包装成RuntimeException抛给客户端,所以web层instanceof关键字处表达式为false
return new RpcResult(new RuntimeException(StringUtils.toString(exception)));
} catch (Throwable e) {
logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost()
+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
+ ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
return result;
}
}
return result;
} catch (RuntimeException e) {
logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost()
+ ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName()
+ ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
throw e;
}
}
2.2 解决问题
经过上面分析源码即可知道,同样的异常,有的方法抛出去web层可以识别,有的不可以,是因为最后抛到web层的service层的方法签名处未加throws BusinessException抛出异常声明,
dubbo将其封装了一层 Return new RpcResult(new RuntimeException(StringUtils.toString(exception)));所以全局异常处理类就捕获不到我们自定义的异常了,走了else的逻辑。
3.总结
在最后抛到web层的service层的方法签名处添加throws BusinessException抛出异常声明
(BusinessException为各自系统自定义异常名称)。
ref:
https://blog.csdn.net/qq_22929803/article/details/52356879?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
————————————————
版权声明:本文为CSDN博主「你存在~我脑海里」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37188616/article/details/104678369