You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/02/28 08:32:17 UTC

[dubbo] branch master updated: GenericException compatible with 2.6.x or lower version (#5800)

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d88487  GenericException compatible with 2.6.x or lower version (#5800)
0d88487 is described below

commit 0d88487fe0fd1d439607c90780481eaf4aab629d
Author: ken.lj <ke...@gmail.com>
AuthorDate: Fri Feb 28 16:32:00 2020 +0800

    GenericException compatible with 2.6.x or lower version (#5800)
    
    fixes #4675
---
 .../dubbo/rpc/service/GenericException.java        | 35 ++++++++++++++++++++--
 .../org/apache/dubbo/rpc/filter/GenericFilter.java | 12 ++++++--
 .../apache/dubbo/rpc/filter/GenericImplFilter.java |  5 ++--
 .../dubbo/rpc/filter/GenericImplFilterTest.java    |  2 +-
 4 files changed, 45 insertions(+), 9 deletions(-)

diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/service/GenericException.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/service/GenericException.java
index 29c20ad..1a94ceb 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/service/GenericException.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/com/alibaba/dubbo/rpc/service/GenericException.java
@@ -17,17 +17,46 @@
 
 package com.alibaba.dubbo.rpc.service;
 
+import org.apache.dubbo.common.utils.StringUtils;
+
 @Deprecated
-public class GenericException extends org.apache.dubbo.rpc.service.GenericException {
+public class GenericException extends RuntimeException {
+
+    private static final long serialVersionUID = -1182299763306599962L;
+
+    private String exceptionClass;
+
+    private String exceptionMessage;
 
     public GenericException() {
     }
 
     public GenericException(String exceptionClass, String exceptionMessage) {
-        super(exceptionClass, exceptionMessage);
+        super(exceptionMessage);
+        this.exceptionClass = exceptionClass;
+        this.exceptionMessage = exceptionMessage;
     }
 
     public GenericException(Throwable cause) {
-        super(cause);
+        super(StringUtils.toString(cause));
+        this.exceptionClass = cause.getClass().getName();
+        this.exceptionMessage = cause.getMessage();
     }
+
+    public String getExceptionClass() {
+        return exceptionClass;
+    }
+
+    public void setExceptionClass(String exceptionClass) {
+        this.exceptionClass = exceptionClass;
+    }
+
+    public String getExceptionMessage() {
+        return exceptionMessage;
+    }
+
+    public void setExceptionMessage(String exceptionMessage) {
+        this.exceptionMessage = exceptionMessage;
+    }
+
 }
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
index 3f3b0e8..cdb03ac 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java
@@ -164,8 +164,16 @@ public class GenericFilter implements Filter, Filter.Listener2 {
                 generic = (String) RpcContext.getContext().getAttachment(GENERIC_KEY);
             }
 
-            if (appResponse.hasException() && !(appResponse.getException() instanceof GenericException)) {
-                appResponse.setException(new GenericException(appResponse.getException()));
+            if (appResponse.hasException()) {
+                Throwable appException = appResponse.getException();
+                if (appException instanceof GenericException) {
+                    GenericException tmp = (GenericException) appException;
+                    appException = new com.alibaba.dubbo.rpc.service.GenericException(tmp.getExceptionClass(), tmp.getExceptionMessage());
+                }
+                if (!(appException instanceof com.alibaba.dubbo.rpc.service.GenericException)) {
+                    appException = new com.alibaba.dubbo.rpc.service.GenericException(appException);
+                }
+                appResponse.setException(appException);
             }
             if (ProtocolUtils.isJavaGenericSerialization(generic)) {
                 try {
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java
index 0762424..05603b6 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericImplFilter.java
@@ -32,7 +32,6 @@ import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.Result;
 import org.apache.dubbo.rpc.RpcException;
 import org.apache.dubbo.rpc.RpcInvocation;
-import org.apache.dubbo.rpc.service.GenericException;
 import org.apache.dubbo.rpc.service.GenericService;
 import org.apache.dubbo.rpc.support.ProtocolUtils;
 import org.apache.dubbo.rpc.support.RpcUtils;
@@ -169,8 +168,8 @@ public class GenericImplFilter implements Filter, Filter.Listener2 {
                 } catch (NoSuchMethodException e) {
                     throw new RpcException(e.getMessage(), e);
                 }
-            } else if (appResponse.getException() instanceof GenericException) {
-                GenericException exception = (GenericException) appResponse.getException();
+            } else if (appResponse.getException() instanceof com.alibaba.dubbo.rpc.service.GenericException) {
+                com.alibaba.dubbo.rpc.service.GenericException exception = (com.alibaba.dubbo.rpc.service.GenericException) appResponse.getException();
                 try {
                     String className = exception.getExceptionClass();
                     Class<?> clazz = ReflectUtils.forName(className);
diff --git a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java
index 258a761..0986b05 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/GenericImplFilterTest.java
@@ -23,11 +23,11 @@ import org.apache.dubbo.rpc.Invocation;
 import org.apache.dubbo.rpc.Invoker;
 import org.apache.dubbo.rpc.Result;
 import org.apache.dubbo.rpc.RpcInvocation;
-import org.apache.dubbo.rpc.service.GenericException;
 import org.apache.dubbo.rpc.service.GenericService;
 import org.apache.dubbo.rpc.support.DemoService;
 import org.apache.dubbo.rpc.support.Person;
 
+import com.alibaba.dubbo.rpc.service.GenericException;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;