You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "gebizhuxiaowang (via GitHub)" <gi...@apache.org> on 2023/04/12 03:02:18 UTC

[GitHub] [dubbo] gebizhuxiaowang opened a new issue, #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

gebizhuxiaowang opened a new issue, #12076:
URL: https://github.com/apache/dubbo/issues/12076

   
   ### 目的:
    在provider端 使用filter 拦截provider 处理过程的异常; 在业务代码中抛出异常后,没有按预期执行 filter 中onResponse 方法,而是执行了onError 方法,在onError 方法中,又无法对结果或异常信息进行包装改写;
   
   问题项目示例地址:https://github.com/gebizhuxiaowang/dubbo-demo
   使用postMan调用gRPC 接口:helloException 可复现;
   
   ### 求助:
   - 在服务端拦截全局异常的方法是否正确?
   - 若正确,上述例子中为什么无法预期执行对应的方法?
   
   
   
   filter 代码如下:
   ```
   package com.example.dubbo.filter;
   
   import org.apache.dubbo.common.constants.CommonConstants;
   import org.apache.dubbo.common.extension.Activate;
   import org.apache.dubbo.common.logger.Logger;
   import org.apache.dubbo.common.logger.LoggerFactory;
   import org.apache.dubbo.common.utils.ReflectUtils;
   import org.apache.dubbo.common.utils.StringUtils;
   import org.apache.dubbo.rpc.Filter;
   import org.apache.dubbo.rpc.Invocation;
   import org.apache.dubbo.rpc.Invoker;
   import org.apache.dubbo.rpc.Result;
   import org.apache.dubbo.rpc.RpcContext;
   import org.apache.dubbo.rpc.RpcException;
   import org.apache.dubbo.rpc.service.GenericService;
   
   import java.lang.reflect.Method;
   
   /**
    * @version 1.0
    * @classname DubboProviderExceptionFilter
    * @description todo
    */
   @Activate(group = CommonConstants.PROVIDER)
   public class DubboProviderExceptionFilter implements Filter, Filter.Listener {
   
       private static Logger log = LoggerFactory.getLogger(DubboProviderExceptionFilter.class);
   
       @Override
       public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
           // return invoker.invoke(invocation);
           Result appResponse = invoker.invoke(invocation);
           if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
               try {
                   Throwable e = appResponse.getException();
                   log.error(" [MAGICAL-DUBBO] Fail to MagicalDubboRpcExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
               } catch (Throwable e) {
                   log.error(" [MAGICAL-DUBBO] Fail to MagicalDubboRpcExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
                   // return AsyncRpcResult.newDefaultAsyncResult(R.fail(65510, "远程服务调用发生异常"), invocation);
               }
           }
           return appResponse;
       }
   
       // 业务代码抛异常后,不会进入这个方法
       @Override
       public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
           if (appResponse.hasException() && GenericService.class != invoker.getInterface()) {
               try {
                   Throwable exception = appResponse.getException();
   
                   // 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.
                   log.error("Got unchecked and undeclared exception which called by " + RpcContext.getServiceContext().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;
                   }
                   // directly throw if it's JDK exception
                   String className = exception.getClass().getName();
                   if (className.startsWith("java.") || className.startsWith("javax.")) {
                       return;
                   }
   
                   // directly throw if it's dubbo exception
                   if (exception instanceof RpcException) {
                       return;
                   }
   
                   // otherwise, wrap with RuntimeException and throw back to the client
                   appResponse.setException(new RuntimeException(StringUtils.toString(exception)));
               } catch (Throwable e) {
                   log.warn("Fail to ExceptionFilter when called by " + RpcContext.getServiceContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e);
               }
           }
       }
   
       @Override
       public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) {
           log.error("Got unchecked and undeclared exception which called by " + RpcContext.getServiceContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + t.getClass().getName() + ": " + t.getMessage(), t);
   
       }
   }
   
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] gebizhuxiaowang commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "gebizhuxiaowang (via GitHub)" <gi...@apache.org>.
gebizhuxiaowang commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1507971141

   我用postman 调用 greetException ,exceptionfilter 是不会进onResponse 方法的;但跑测试用例是可以的。
    您可以试下; 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1506983837

   > > 升级到最新的版本试试
   > 
   > 正式版从3.1.5 到3.1.9 beta版 3.2.0-beta.4 到3.2.0-beta.6 都试了,不好使.
   > 
   > 对了,业务处理类抛出的异常是自定义异常; @EarthChen
   
   <img width="1404" alt="image" src="https://user-images.githubusercontent.com/20179425/231776416-3dd2a4bc-3bdb-4a80-a977-9a834366368b.png">
   
   我这里是可以的,你可以使用https://github.com/apache/dubbo-samples/blob/master/3-extensions/protocol/dubbo-samples-triple/src/test/java/org/apache/dubbo/sample/tri/BaseTriPojoClientTest.java  中的`org.apache.dubbo.sample.tri.BaseTriPojoClientTest#greetException` 试试
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] AlbumenJ commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "AlbumenJ (via GitHub)" <gi...@apache.org>.
AlbumenJ commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1506941908

   @EarthChen PTAL


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] gebizhuxiaowang commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "gebizhuxiaowang (via GitHub)" <gi...@apache.org>.
gebizhuxiaowang commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1504756878

   > 升级到最新的版本试试
   
   正式版从3.1.5 到3.1.9
   beta版 3.2.0-beta.4 到3.2.0-beta.6 都试了,不好使.
   
   对了,业务处理类抛出的异常是自定义异常;
   @EarthChen 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1508102933

   > 原始诉求就是统一拦截服务端异常;这个应该怎么实现 😂
   
   知道问题在哪里了,stub 模式下的处理问题


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1507974762

   > 我用postman 调用 greetException ,exceptionfilter 是不会进onResponse 方法的;但跑测试用例是可以的。 您可以试下;
   
   这是个 wrapper 模式的接口,你用 postman 调用应该调用不通把


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] gebizhuxiaowang commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "gebizhuxiaowang (via GitHub)" <gi...@apache.org>.
gebizhuxiaowang commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1508022852

   搞错了,它俩调用不是一个实现类 😂
   postman 调用的是 `org.apache.dubbo.sample.tri.stub.GreeterImpl#greetException`
   postman 调用这个greetException ,exceptionfilter 也是不会进onResponse 方法的;这正常么?
   ![image](https://user-images.githubusercontent.com/16876323/231967808-8c876aba-7ed0-4af7-87bb-938ad0caeeb4.png)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] gebizhuxiaowang commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "gebizhuxiaowang (via GitHub)" <gi...@apache.org>.
gebizhuxiaowang commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1508037939

   原始诉求就是统一拦截服务端异常;这个应该怎么实现 😂


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] gebizhuxiaowang commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "gebizhuxiaowang (via GitHub)" <gi...@apache.org>.
gebizhuxiaowang commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1507872839

   > 我这里是可以的,你可以使用https://github.com/apache/dubbo-samples/blob/master/3-extensions/protocol/dubbo-samples-triple/src/test/java/org/apache/dubbo/sample/tri/BaseTriPojoClientTest.java 中的org.apache.dubbo.sample.tri.BaseTriPojoClientTest#greetException 试试
   
   我跑了这个测试用例,发现可以正常进入`onResponse`;
   
   对比下我自己的工程,他们的差异在于:我是用gprc-stub 来调用的,就是用postman 来进行调用测试的;而您提供这个示例中是用duubo-stub ;
   
   这似乎是个问题?@EarthChen 
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1508160304

   > 所以这是个问题,会在未来的版本修复?
   
   stub 模式下是将用户的逻辑当做一个 func 去执行,当抛出异常时,无法区分是框架异常还是用户异常


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1507883482

   > > 我这里是可以的,你可以使用https://github.com/apache/dubbo-samples/blob/master/3-extensions/protocol/dubbo-samples-triple/src/test/java/org/apache/dubbo/sample/tri/BaseTriPojoClientTest.java 中的org.apache.dubbo.sample.tri.BaseTriPojoClientTest#greetException 试试
   > 
   > 我跑了这个测试用例,发现可以正常进入`onResponse`;
   > 
   > 对比下我自己的工程,他们的差异在于:我是用grpc-stub 来调用的,就是用postman 来进行调用测试的;而您提供这个示例中是用duubo-stub ;
   > 
   > 这似乎是个问题?@EarthChen
   
   没有区别的  这个filter 是工作在 provider 端,对于你调用端是什么 不重要的


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] gebizhuxiaowang commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "gebizhuxiaowang (via GitHub)" <gi...@apache.org>.
gebizhuxiaowang commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1508137557

   所以这是个问题,会在未来的版本修复?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1504739090

   升级到最新的版本试试


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] liufeiyu1002 commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "liufeiyu1002 (via GitHub)" <gi...@apache.org>.
liufeiyu1002 commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1506590868

   我认为这个应该是符合预期的,
   图1
   ![image](https://user-images.githubusercontent.com/32605119/231703967-ada2911b-ed87-42fe-a9aa-308f67b44bcc.png)
   图2
   ![image](https://user-images.githubusercontent.com/32605119/231704650-f3215540-bd32-457b-ba5f-17182cb9e94b.png)
   图3
   ![image](https://user-images.githubusercontent.com/32605119/231705554-af7f93d1-b2aa-4332-84cd-a8e69b90e34e.png)
   1.  `doInvoke` 里会通过反射调用业务方法,当业务方法抛出异常时 会被后边的 catch 捕获如  3位置所示, 此时 t!=null 
   2. 看图2 `wrapWithFuture` 调用以后会对结果包装 分3 中情况 1. 接口本身返回是CompletedFuture类型的 2. 调用时异步的 3.包装一个completedFuture返回  ---你的情况应该是 3 这种
   3. 图3 中能看到执行 `onError` 还是 `onResponse` 是按照 t 是否为null 来判断的
   4. 在 `OnResponse` 中判断 `appResponse.hasException()` 可以对应到2结果返回是 如果接口本身返回是`CompletedFuture`类型。接口正常返回但是结果是 `error` 的


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen closed issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen closed issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行
URL: https://github.com/apache/dubbo/issues/12076


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


[GitHub] [dubbo] EarthChen commented on issue #12076: dubbo-java 3.2 beta 服务端全局异常处理filter 无法按预期执行

Posted by "EarthChen (via GitHub)" <gi...@apache.org>.
EarthChen commented on issue #12076:
URL: https://github.com/apache/dubbo/issues/12076#issuecomment-1506995213

   之前对这个问题修过一次  https://github.com/apache/dubbo/pull/11133/files


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org