Don't overwrite global err before return (#1293)

Signed-off-by: Olivier Charvin <git@olivier.pfad.fr>
This commit is contained in:
oliverpool 2020-08-25 14:11:52 +02:00 committed by GitHub
parent c8b873abc8
commit a4db43e096
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 64 deletions

View file

@ -38,9 +38,18 @@ type Transaction interface {
// was applied correctly. For example, 'database is locked' errors in sqlite will happen here.
func EndTransaction(txn Transaction, succeeded *bool) error {
if *succeeded {
return txn.Commit() // nolint: errcheck
return txn.Commit()
} else {
return txn.Rollback() // nolint: errcheck
return txn.Rollback()
}
}
// EndTransactionWithCheck ends a transaction and overwrites the error pointer if its value was nil.
// If the transaction succeeded then it is committed, otherwise it is rolledback.
// Designed to be used with defer (see EndTransaction otherwise).
func EndTransactionWithCheck(txn Transaction, succeeded *bool, err *error) {
if e := EndTransaction(txn, succeeded); e != nil && *err == nil {
*err = e
}
}
@ -53,12 +62,7 @@ func WithTransaction(db *sql.DB, fn func(txn *sql.Tx) error) (err error) {
return fmt.Errorf("sqlutil.WithTransaction.Begin: %w", err)
}
succeeded := false
defer func() {
err2 := EndTransaction(txn, &succeeded)
if err == nil && err2 != nil { // failed to commit/rollback
err = err2
}
}()
defer EndTransactionWithCheck(txn, &succeeded, &err)
err = fn(txn)
if err != nil {