From f32e7c5557bc14ee5892cb3bd4693456c8503c3a Mon Sep 17 00:00:00 2001 From: oldrat Date: Tue, 24 Jul 2012 10:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E4=BF=A1=E6=81=AF=E5=8A=A0?= =?UTF-8?q?=E5=BC=BA=20=E5=92=8C=20bug=20fix=20DUBBO-510=20Provider?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=9C=89Error=E6=97=B6=EF=BC=8CProvider?= =?UTF-8?q?=E7=AB=AF=E6=B2=A1=E6=9C=89=E6=97=A5=E5=BF=97=E6=8A=A5=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dubbo/rpc/filter/ExceptionFilter.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ExceptionFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ExceptionFilter.java index 854a2bcef1f..f343a96f37a 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ExceptionFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/filter/ExceptionFilter.java @@ -34,8 +34,17 @@ /** * ExceptionInvokerFilter + *

+ * 功能: + *

    + *
  1. 不期望的异常打ERROR日志(Provider端)
    + * 不期望的日志即是,没有的接口上声明的Unchecked异常。 + *
  2. 异常不在API包中,则Wrap一层RuntimeException。
    + * RPC对于第一层异常会直接序列化传输(Cause异常会String化),避免异常在Client出不能反序列化问题。 + *
* * @author william.liangf + * @author ding.lid */ @Activate(group = Constants.PROVIDER) public class ExceptionFilter implements Filter { @@ -56,8 +65,9 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept if (result.hasException() && GenericService.class != invoker.getInterface()) { try { Throwable exception = result.getException(); + // 如果是checked异常,直接抛出 - if (! (exception instanceof RuntimeException)) { + if (! (exception instanceof RuntimeException) && (exception instanceof Exception)) { return result; } // 在方法签名上有声明,直接抛出 @@ -72,8 +82,12 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept } catch (NoSuchMethodException e) { return result; } + // 未在方法签名上定义的异常,在服务器端打印ERROR日志 - 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); + 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()); @@ -89,16 +103,21 @@ public Result invoke(Invoker invoker, Invocation invocation) throws RpcExcept if (exception instanceof RpcException) { return result; } + // 否则,包装成RuntimeException抛给客户端 return new RpcResult(new RuntimeException(StringUtils.toString(exception))); } catch (Throwable e) { - logger.warn(e.getMessage(), 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); + 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; } }