Handle rollbackError with log.Fatalf()
This commit is contained in:
parent
ea45da66b7
commit
052d36b01b
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user