fix: defer functions overriding `err` return value

This commit is contained in:
Patryk Osmaczko 2022-04-12 18:21:31 +02:00 committed by osmaczko
parent 907ba8ee5c
commit 5e2a33115c
1 changed files with 14 additions and 4 deletions

View File

@ -55,7 +55,8 @@ func (db *Database) GetAccounts() (rst []Account, err error) {
return nil, err
}
defer func() {
err = rows.Close()
errClose := rows.Close()
err = valueOr(err, errClose)
}()
for rows.Next() {
@ -175,7 +176,8 @@ func (db *Database) GetIdentityImages(keyUID string) (iis []*images.IdentityImag
return nil, err
}
defer func() {
err = rows.Close()
errClose := rows.Close()
err = valueOr(err, errClose)
}()
for rows.Next() {
@ -213,8 +215,9 @@ func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityIma
err = tx.Commit()
return
}
// don't shadow original error
_ = tx.Rollback()
errRollback := tx.Rollback()
err = valueOr(err, errRollback)
}()
for _, ii := range iis {
@ -264,3 +267,10 @@ func (db *Database) DeleteIdentityImage(keyUID string) error {
_, err := db.db.Exec(`DELETE FROM identity_images WHERE key_uid = ?`, keyUID)
return err
}
func valueOr(value error, or error) error {
if value != nil {
return value
}
return or
}