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

zfeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-seata-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 22f5f7c2 fix:The WithGlobalTx method returns the exception caught by 
recover (#849)
22f5f7c2 is described below

commit 22f5f7c27bf42933784545bb004e8e0fd24d954f
Author: tanzegen <39859116+tanze...@users.noreply.github.com>
AuthorDate: Sat Aug 30 15:20:42 2025 +0800

    fix:The WithGlobalTx method returns the exception caught by recover (#849)
    
    * fix:The WithGlobalTx method returns the exception caught by recover
    
    * fix:add case of panic non-error type;WithGlobalTx return a specific 
exception
    
    * fix:Combine error judgments into a single conditional statement to make 
the code more readable.
    
    ---------
    
    Co-authored-by: FengZhang <zfc...@qq.com>
---
 pkg/tm/transaction_executor.go      | 14 ++++++++++++--
 pkg/tm/transaction_executor_test.go | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/pkg/tm/transaction_executor.go b/pkg/tm/transaction_executor.go
index 93024160..69df09ed 100644
--- a/pkg/tm/transaction_executor.go
+++ b/pkg/tm/transaction_executor.go
@@ -73,8 +73,18 @@ func WithGlobalTx(ctx context.Context, gc *GtxConfig, 
business CallbackWithCtx)
                        }
                }
 
-               if re != nil || err != nil {
-                       re = fmt.Errorf("first phase error: %v, second phase 
error: %v", re, err)
+               if re == nil {
+                       if deferErr != nil {
+                               // if deferErr is not an error, then convert it 
to error
+                               // this is because panic may be caused by 
non-error type e.g. panic("some string")
+                               // so we need to convert it to error type
+                               if _, ok := deferErr.(error); !ok {
+                                       deferErr = fmt.Errorf("%v", deferErr)
+                               }
+                               re = deferErr.(error)
+                       } else if err != nil {
+                               re = err
+                       }
                }
        }()
 
diff --git a/pkg/tm/transaction_executor_test.go 
b/pkg/tm/transaction_executor_test.go
index fec5030a..277f2397 100644
--- a/pkg/tm/transaction_executor_test.go
+++ b/pkg/tm/transaction_executor_test.go
@@ -382,6 +382,12 @@ func TestWithGlobalTx(t *testing.T) {
        callbackNil := func(ctx context.Context) error {
                return nil
        }
+       callbackPanicError := func(ctx context.Context) error {
+               panic(errors.New("mock callback panic error"))
+       }
+       callbackPanicString := func(ctx context.Context) error {
+               panic("mock callback panic string")
+       }
 
        type testCase struct {
                GtxConfig              *GtxConfig
@@ -458,6 +464,34 @@ func TestWithGlobalTx(t *testing.T) {
                        },
                },
 
+               // case callback panic string
+               {
+                       GtxConfig: &GtxConfig{
+                               Name: "MockGtxConfig",
+                       },
+                       callbackErr:     true,
+                       callback:        callbackPanicString,
+                       occurError:      true,
+                       errMessage:      "mock callback panic string",
+                       mockBeginTarget: begin,
+                       mockBeginFunc: func(ctx context.Context, gc *GtxConfig) 
error {
+                               return nil
+                       },
+               },
+
+               // case callback panic error
+               {
+                       GtxConfig: &GtxConfig{
+                               Name: "MockGtxConfig",
+                       },
+                       callbackErr:     true,
+                       callback:        callbackPanicError,
+                       mockBeginTarget: begin,
+                       mockBeginFunc: func(ctx context.Context, gc *GtxConfig) 
error {
+                               return nil
+                       },
+               },
+
                // case second mock error
                {
                        GtxConfig: &GtxConfig{


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org
For additional commands, e-mail: notifications-h...@seata.apache.org

Reply via email to