From e7f45d5b0112fdce04b479d27f60c8dd8c66f3c0 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Thu, 19 Nov 2020 10:45:17 +0800 Subject: [PATCH] Add error check for Transaction --- finisher_api.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index 1efa2e46..f2aed8da 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -472,7 +472,7 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er if committer, ok := db.Statement.ConnPool.(TxCommitter); ok && committer != nil { // nested transaction - db.SavePoint(fmt.Sprintf("sp%p", fc)) + err = db.SavePoint(fmt.Sprintf("sp%p", fc)).Error defer func() { // Make sure to rollback when panic, Block error or Commit error if panicked || err != nil { @@ -480,7 +480,9 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er } }() - err = fc(db.Session(&Session{})) + if err == nil { + err = fc(db.Session(&Session{})) + } } else { tx := db.Begin(opts...) @@ -491,7 +493,9 @@ func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err er } }() - err = fc(tx) + if err = tx.Error; err == nil { + err = fc(tx) + } if err == nil { err = tx.Commit().Error