From dc37aaceea903fcc84144c7566a14b3c5378dcaf Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 24 Oct 2024 17:01:24 +0200 Subject: Rework error handling of model.DB.Transaction() The functions in the sub-packages model, view and controller should not log too much. Instead they should return errors which can be extended by fmt.Errorf(": %w", err). --- model/db.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/model/db.go b/model/db.go index f3fb607..3746f71 100644 --- a/model/db.go +++ b/model/db.go @@ -28,8 +28,7 @@ func OpenDB(path string) (*DB, error) { func (db *DB) Transaction(f func(*sql.Tx) error) error { tx, err := (*sql.DB)(db).Begin() if err != nil { - log.Printf("Failed to start database transaction: %v", err) - return err + return fmt.Errorf("Failed to start database transaction: %w", err) } defer func() { if tx.Rollback() == nil { @@ -39,8 +38,7 @@ func (db *DB) Transaction(f func(*sql.Tx) error) error { err = f(tx) if err != nil { - log.Printf("Failed transaction: %v", err) - return err + return fmt.Errorf("Failed transaction: %w", err) } return tx.Commit() -- cgit v1.2.3-70-g09d2