// 如果是受检异常,不处理 // directly throw if it's checked exception if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) { return; } // directly throw if the exception appears in the signature try { // 获取服务接口上的异常声明 Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes()); Class<?>[] exceptionClasses = method.getExceptionTypes(); for (Class<?> exceptionClass : exceptionClasses) { // 如果是声明的异常,不处理 if (exception.getClass().equals(exceptionClass)) { return; } } } catch (NoSuchMethodException e) { return; }
// for the exception not found in method's signature, print ERROR message in server's log. 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);
// 异常和接口是不在同一个包下,如果是的话,不处理 // directly throw if exception class and interface class are in the same jar file. String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface()); String exceptionFile = ReflectUtils.getCodeBase(exception.getClass()); if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) { return; } // 如果是 JDK 内置异常类型,不处理 // directly throw if it's JDK exception String className = exception.getClass().getName(); if (className.startsWith("java.") || className.startsWith("javax.")) { return; } // 如果是 RpcException,不处理 // directly throw if it's dubbo exception if (exception instanceof RpcException) { return; }
// otherwise, wrap with RuntimeException and throw back to the client // 其他异常,将其包装为 RunctionException 并返回 appResponse.setException(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); } } }