This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new bd3af98410 generic_exception_support_transfer (#10382)
bd3af98410 is described below
commit bd3af9841082c144009604d9a9b3d0266ed3405b
Author: Owen.Cai <[email protected]>
AuthorDate: Wed Aug 17 21:28:26 2022 +0800
generic_exception_support_transfer (#10382)
* generic_exception_support_transfer
* fix getmessage bug
* fastjson parse no message
* generic exception add uts
* fix null check
* add header & delete auther info
---
.../apache/dubbo/rpc/service/GenericException.java | 89 +++++++++++++++++-----
.../dubbo/rpc/service/GenericExceptionTest.java | 59 ++++++++++++++
2 files changed, 130 insertions(+), 18 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
index 9f99ecac54..1855b28ed5 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/rpc/service/GenericException.java
@@ -20,7 +20,7 @@ import org.apache.dubbo.common.utils.JsonUtils;
import org.apache.dubbo.common.utils.StringUtils;
import java.beans.Transient;
-
+import java.io.Serializable;
/**
* GenericException
@@ -37,8 +37,10 @@ public class GenericException extends RuntimeException {
private String exceptionMessage;
+ private final GenericExceptionInfo genericExceptionInfo;
+
public GenericException() {
- this.useCause = false;
+ this(null, null);
}
public GenericException(String exceptionClass, String exceptionMessage) {
@@ -46,13 +48,7 @@ public class GenericException extends RuntimeException {
this.useCause = false;
this.exceptionClass = exceptionClass;
this.exceptionMessage = exceptionMessage;
- }
-
- public GenericException(String exceptionClass, String exceptionMessage,
String message) {
- super(message);
- this.useCause = false;
- this.exceptionClass = exceptionClass;
- this.exceptionMessage = exceptionMessage;
+ this.genericExceptionInfo = new GenericExceptionInfo(exceptionClass,
exceptionMessage, exceptionMessage, getStackTrace());
}
public GenericException(Throwable cause) {
@@ -60,43 +56,83 @@ public class GenericException extends RuntimeException {
this.useCause = false;
this.exceptionClass = cause.getClass().getName();
this.exceptionMessage = cause.getMessage();
+ this.genericExceptionInfo = new
GenericExceptionInfo(this.exceptionClass, this.exceptionMessage,
super.getMessage(), getStackTrace());
+ }
+
+ protected GenericException(GenericExceptionInfo info) {
+ super(info.getMsg(), null, true, false);
+ setStackTrace(info.getStackTrace());
+ this.useCause = false;
+ this.exceptionClass = info.getExClass();
+ this.exceptionMessage = info.getExMsg();
+ this.genericExceptionInfo = info;
}
@Transient
public String getExceptionClass() {
+ if(this.useCause) {
+ return ((GenericException)getCause()).getExceptionClass();
+ }
return exceptionClass;
}
public void setExceptionClass(String exceptionClass) {
+ if(this.useCause) {
+ ((GenericException)getCause()).setExceptionClass(exceptionClass);
+ return;
+ }
this.exceptionClass = exceptionClass;
}
@Transient
public String getExceptionMessage() {
+ if(this.useCause) {
+ return ((GenericException)getCause()).getExceptionMessage();
+ }
return exceptionMessage;
}
public void setExceptionMessage(String exceptionMessage) {
+ if(this.useCause) {
+
((GenericException)getCause()).setExceptionMessage(exceptionMessage);
+ return;
+ }
this.exceptionMessage = exceptionMessage;
}
@Override
+ @Transient
+ public StackTraceElement[] getStackTrace() {
+ if(this.useCause) {
+ return ((GenericException)getCause()).getStackTrace();
+ }
+ return super.getStackTrace();
+ }
+
+ @Override
+ @Transient
public String getMessage() {
if(this.useCause) {
- Throwable cause = getCause();
- if(cause != null) {
- return cause.getMessage();
- }
+ return getCause().getMessage();
+ }
+ return
JsonUtils.getJson().toJson(GenericExceptionInfo.createNoStackTrace(genericExceptionInfo));
+ }
+
+ public String getGenericException() {
+ if(this.useCause) {
+ return ((GenericException)getCause()).getGenericException();
}
- GenericExceptionInfo genericExceptionInfo = new
GenericExceptionInfo(exceptionClass, exceptionMessage, getMessage());
return JsonUtils.getJson().toJson(genericExceptionInfo);
}
- public void setMessage(String json) {
+ public void setGenericException(String json) {
GenericExceptionInfo info = JsonUtils.getJson().toJavaObject(json,
GenericExceptionInfo.class);
+ if(info == null) {
+ return;
+ }
this.useCause = true;
- initCause(new GenericException(info.getExClass(), info.getExMsg(),
info.getMsg()));
+ initCause(new GenericException(info));
}
@Override
@@ -108,15 +144,24 @@ public class GenericException extends RuntimeException {
/**
* create generic exception info
*/
- public static class GenericExceptionInfo {
+ public static class GenericExceptionInfo implements Serializable {
private String exClass;
private String exMsg;
private String msg;
+ private StackTraceElement[] stackTrace;
+
+ public GenericExceptionInfo() {
+ }
- public GenericExceptionInfo(String exceptionClass, String
exceptionMessage, String message) {
+ public GenericExceptionInfo(String exceptionClass, String
exceptionMessage, String message, StackTraceElement[] stackTrace) {
this.exClass = exceptionClass;
this.exMsg = exceptionMessage;
this.msg = message;
+ this.stackTrace = stackTrace;
+ }
+
+ public static GenericExceptionInfo
createNoStackTrace(GenericExceptionInfo info) {
+ return new GenericExceptionInfo(info.getExClass(),
info.getExMsg(), info.getMsg(), null);
}
public String getMsg() {
@@ -142,5 +187,13 @@ public class GenericException extends RuntimeException {
public void setMsg(String msg) {
this.msg = msg;
}
+
+ public StackTraceElement[] getStackTrace() {
+ return stackTrace;
+ }
+
+ public void setStackTrace(StackTraceElement[] stackTrace) {
+ this.stackTrace = stackTrace;
+ }
}
}
diff --git
a/dubbo-common/src/test/java/org/apache/dubbo/rpc/service/GenericExceptionTest.java
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/service/GenericExceptionTest.java
new file mode 100644
index 0000000000..edbbfc57e9
--- /dev/null
+++
b/dubbo-common/src/test/java/org/apache/dubbo/rpc/service/GenericExceptionTest.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.rpc.service;
+
+import org.apache.dubbo.common.utils.JsonUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+public class GenericExceptionTest {
+
+ @Test
+ void jsonSupport() throws IOException {
+ {
+ GenericException src = new GenericException();
+ String s = JsonUtils.getJson().toJson(src);
+ GenericException dst = JsonUtils.getJson().toJavaObject(s,
GenericException.class);
+ Assertions.assertEquals(src.getExceptionClass(),
dst.getExceptionClass());
+ Assertions.assertEquals(src.getExceptionMessage(),
dst.getExceptionMessage());
+ Assertions.assertEquals(src.getMessage(), dst.getMessage());
+ Assertions.assertEquals(src.getExceptionMessage(),
dst.getExceptionMessage());
+ }
+ {
+ GenericException src = new
GenericException(this.getClass().getSimpleName(), "test");
+ String s = JsonUtils.getJson().toJson(src);
+ GenericException dst = JsonUtils.getJson().toJavaObject(s,
GenericException.class);
+ Assertions.assertEquals(src.getExceptionClass(),
dst.getExceptionClass());
+ Assertions.assertEquals(src.getExceptionMessage(),
dst.getExceptionMessage());
+ Assertions.assertEquals(src.getMessage(), dst.getMessage());
+ Assertions.assertEquals(src.getExceptionMessage(),
dst.getExceptionMessage());
+ }
+ {
+ Throwable throwable = new Throwable("throwable");
+ GenericException src = new GenericException(throwable);
+ String s = JsonUtils.getJson().toJson(src);
+ GenericException dst = JsonUtils.getJson().toJavaObject(s,
GenericException.class);
+ Assertions.assertEquals(src.getExceptionClass(),
dst.getExceptionClass());
+ Assertions.assertEquals(src.getExceptionMessage(),
dst.getExceptionMessage());
+ Assertions.assertEquals(src.getMessage(), dst.getMessage());
+ Assertions.assertEquals(src.getExceptionMessage(),
dst.getExceptionMessage());
+ }
+ }
+}