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

jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 7739e5086f optimize: add ExceptionUtil class for unwarp error msg 
(#6666)
7739e5086f is described below

commit 7739e5086f49b3e9a13255bb475dc08dc60d4978
Author: iAmClever <[email protected]>
AuthorDate: Fri Jul 12 16:15:02 2024 +0800

    optimize: add ExceptionUtil class for unwarp error msg (#6666)
---
 changes/en-us/2.x.md                               |  3 +-
 changes/zh-cn/2.x.md                               |  3 +-
 .../seata/common/exception/ExceptionUtil.java      | 40 ++++++++++++++++++++++
 .../apache/seata/rm/fence/SpringFenceHandler.java  | 29 +++++++++-------
 .../apache/seata/rm/tcc/TCCResourceManager.java    |  5 +--
 5 files changed, 64 insertions(+), 16 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index 3acfe9df7f..cc56c09952 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -24,6 +24,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#6534](https://github.com/apache/incubator-seata/pull/6534)] optimize: 
send async response
 - [[#6640](https://github.com/apache/incubator-seata/pull/6640)] modify 
codecov config
 - [[#6640](https://github.com/apache/incubator-seata/pull/6648)] add license 
header
+- [[#6666](https://github.com/apache/incubator-seata/pull/6666)] add 
ExceptionUtil class for unwarp error msg
 - [[#6654](https://github.com/apache/incubator-seata/pull/6654)] add 
Namingserver package module
 - [[#6667](https://github.com/apache/incubator-seata/pull/6667)] optimize 
Namingserver log output
 
@@ -52,6 +53,6 @@ Thanks to these contributors for their code commits. Please 
report an unintended
 - [xjlgod](https://github.com/xjlgod)
 - [xingfudeshi](https://github.com/xingfudeshi)
 - [wuwen5](https://github.com/wuwen5)
-
+- [iAmClever(https://github.com/iAmClever)
 
 Also, we receive many valuable issues, questions and advices from our 
community. Thanks for you all.
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 52f808c202..43991dbf04 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -24,6 +24,7 @@
 - [[#6566](https://github.com/apache/incubator-seata/pull/6566)] 
支持GlobalTransactionScanner类中exposeProxy属性的配置
 - [[#6534](https://github.com/apache/incubator-seata/pull/6534)] 优化: 发送异步响应
 - [[#6534](https://github.com/apache/incubator-seata/pull/6648)] 增加license 
header信息
+- [[#6666](https://github.com/apache/incubator-seata/pull/6666)] 
添加ExceptionUtil工具类用于解包装异常
 - [[#6654](https://github.com/apache/incubator-seata/pull/6654)] 
增加Namingserver打包功能
 - [[#6667](https://github.com/apache/incubator-seata/pull/6667)] 
优化Namingserver日志输出
 
@@ -53,6 +54,6 @@
 - [xjlgod](https://github.com/xjlgod)
 - [xingfudeshi](https://github.com/xingfudeshi)
 - [wuwen5](https://github.com/wuwen5)
-
+- [iAmClever](https://github.com/iAmClever)
 
 同时,我们收到了社区反馈的很多有价值的issue和建议,非常感谢大家。
diff --git 
a/common/src/main/java/org/apache/seata/common/exception/ExceptionUtil.java 
b/common/src/main/java/org/apache/seata/common/exception/ExceptionUtil.java
new file mode 100644
index 0000000000..a38eab78b4
--- /dev/null
+++ b/common/src/main/java/org/apache/seata/common/exception/ExceptionUtil.java
@@ -0,0 +1,40 @@
+/*
+ * 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.seata.common.exception;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.UndeclaredThrowableException;
+
+public class ExceptionUtil {
+
+    private ExceptionUtil() {
+    }
+
+    public static Throwable unwrap(Throwable wrapped) {
+        Throwable unwrapped = wrapped;
+        while (true) {
+            if (unwrapped instanceof InvocationTargetException) {
+                unwrapped = ((InvocationTargetException) 
unwrapped).getTargetException();
+            } else if (unwrapped instanceof UndeclaredThrowableException) {
+                unwrapped = ((UndeclaredThrowableException) 
unwrapped).getUndeclaredThrowable();
+            } else {
+                return unwrapped;
+            }
+        }
+    }
+
+}
diff --git 
a/spring/src/main/java/org/apache/seata/rm/fence/SpringFenceHandler.java 
b/spring/src/main/java/org/apache/seata/rm/fence/SpringFenceHandler.java
index 12582d6f93..95ada3dc09 100644
--- a/spring/src/main/java/org/apache/seata/rm/fence/SpringFenceHandler.java
+++ b/spring/src/main/java/org/apache/seata/rm/fence/SpringFenceHandler.java
@@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit;
 
 import javax.sql.DataSource;
 
+import org.apache.seata.common.exception.ExceptionUtil;
 import org.apache.seata.common.exception.FrameworkErrorCode;
 import org.apache.seata.common.exception.SkipCallbackWrapperException;
 import org.apache.seata.common.executor.Callback;
@@ -253,21 +254,25 @@ public class SpringFenceHandler implements FenceHandler {
     private static boolean updateStatusAndInvokeTargetMethod(Connection conn, 
Method method, Object targetTCCBean,
                                                              String xid, Long 
branchId, int status,
                                                              TransactionStatus 
transactionStatus,
-                                                             Object[] args) 
throws Exception {
+                                                             Object[] args) 
throws Throwable {
         boolean result = COMMON_FENCE_DAO.updateCommonFenceDO(conn, xid, 
branchId, status, CommonFenceConstant.STATUS_TRIED);
         if (result) {
-            // invoke two phase method
-            Object ret = method.invoke(targetTCCBean, args);
-            if (null != ret) {
-                if (ret instanceof TwoPhaseResult) {
-                    result = ((TwoPhaseResult) ret).isSuccess();
-                } else {
-                    result = (boolean) ret;
-                }
-                // If the business execution result is false, the transaction 
will be rolled back
-                if (!result) {
-                    transactionStatus.setRollbackOnly();
+            try {
+                // invoke two phase method
+                Object ret = method.invoke(targetTCCBean, args);
+                if (null != ret) {
+                    if (ret instanceof TwoPhaseResult) {
+                        result = ((TwoPhaseResult) ret).isSuccess();
+                    } else {
+                        result = (boolean) ret;
+                    }
+                    // If the business execution result is false, the 
transaction will be rolled back
+                    if (!result) {
+                        transactionStatus.setRollbackOnly();
+                    }
                 }
+            } catch (Exception e) {
+                throw ExceptionUtil.unwrap(e);
             }
         }
         return result;
diff --git a/tcc/src/main/java/org/apache/seata/rm/tcc/TCCResourceManager.java 
b/tcc/src/main/java/org/apache/seata/rm/tcc/TCCResourceManager.java
index 2e7286da1f..eaaa76494c 100644
--- a/tcc/src/main/java/org/apache/seata/rm/tcc/TCCResourceManager.java
+++ b/tcc/src/main/java/org/apache/seata/rm/tcc/TCCResourceManager.java
@@ -22,6 +22,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.seata.common.Constants;
+import org.apache.seata.common.exception.ExceptionUtil;
 import org.apache.seata.common.exception.RepeatRegistrationException;
 import org.apache.seata.common.exception.ShouldNeverHappenException;
 import org.apache.seata.common.exception.SkipCallbackWrapperException;
@@ -143,7 +144,7 @@ public class TCCResourceManager extends 
AbstractResourceManager {
             return result ? BranchStatus.PhaseTwo_Committed : 
BranchStatus.PhaseTwo_CommitFailed_Retryable;
         } catch (Throwable t) {
             String msg = String.format("commit TCC resource error, resourceId: 
%s, xid: %s.", resourceId, xid);
-            LOGGER.error(msg, t);
+            LOGGER.error(msg, ExceptionUtil.unwrap(t));
             return BranchStatus.PhaseTwo_CommitFailed_Retryable;
         }
     }
@@ -202,7 +203,7 @@ public class TCCResourceManager extends 
AbstractResourceManager {
             return result ? BranchStatus.PhaseTwo_Rollbacked : 
BranchStatus.PhaseTwo_RollbackFailed_Retryable;
         } catch (Throwable t) {
             String msg = String.format("rollback TCC resource error, 
resourceId: %s, xid: %s.", resourceId, xid);
-            LOGGER.error(msg, t);
+            LOGGER.error(msg, ExceptionUtil.unwrap(t));
             return BranchStatus.PhaseTwo_RollbackFailed_Retryable;
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to