From 052d36b01b4c9091fc74f19aafcef091f6853bf7 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Wed, 6 Mar 2024 16:58:41 +0100 Subject: [PATCH] Handle rollbackError with log.Fatalf() --- cmd/data/db.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/data/db.go b/cmd/data/db.go index 5961a34..84a43af 100644 --- a/cmd/data/db.go +++ b/cmd/data/db.go @@ -3,6 +3,7 @@ package data import ( "database/sql" "fmt" + "log" "github.com/go-sql-driver/mysql" "golang.org/x/crypto/bcrypt" @@ -102,18 +103,24 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error { ` row := tx.QueryRow(getQuery, id) if err := row.Scan(&queriedPass); err != nil { - tx.Rollback() + if rollbackErr := tx.Rollback(); rollbackErr != nil { + log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr) + } return fmt.Errorf("error reading password from DB: %v", err) } if err := bcrypt.CompareHashAndPassword([]byte(queriedPass), []byte(oldPass)); err != nil { - tx.Rollback() + if rollbackErr := tx.Rollback(); rollbackErr != nil { + log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr) + } return fmt.Errorf("incorrect password: %v", err) } newHashedPass, err := bcrypt.GenerateFromPassword([]byte(newPass), bcrypt.DefaultCost) if err != nil { - tx.Rollback() + if rollbackErr := tx.Rollback(); rollbackErr != nil { + log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr) + } return fmt.Errorf("error creating password hash: %v", err) } @@ -123,7 +130,9 @@ func (db *DB) ChangePassword(id int64, oldPass, newPass string) error { WHERE id = ? ` if _, err = tx.Exec(setQuery, string(newHashedPass), id); err != nil { - tx.Rollback() + if rollbackErr := tx.Rollback(); rollbackErr != nil { + log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr) + } return fmt.Errorf("error updating password in DB: %v", err) }