feat(account): #49 refactor error handling

This commit is contained in:
2025-05-08 21:45:53 +02:00
parent 4744da0bee
commit 5cfea4e2d3
14 changed files with 142 additions and 119 deletions

View File

@@ -1,6 +1,7 @@
package db
import (
"database/sql"
"spend-sparrow/log"
"spend-sparrow/types"
@@ -31,7 +32,7 @@ func (db AccountSqlite) Insert(userId uuid.UUID, account *types.Account) error {
INSERT INTO account (id, user_id, name, current_balance, oink_balance, created_at, created_by)
VALUES (?,?,?,?,?,?,?)`, account.Id, userId, account.Name, 0, 0, account.CreatedAt, account.CreatedBy)
if err != nil {
log.Error("Error inserting account: %v", err)
log.Error("account Insert: %v", err)
return types.ErrInternal
}
@@ -40,7 +41,6 @@ func (db AccountSqlite) Insert(userId uuid.UUID, account *types.Account) error {
func (db AccountSqlite) Update(userId uuid.UUID, account *types.Account) error {
log.Info("Updating account: %v", account)
r, err := db.db.Exec(`
UPDATE account
SET
@@ -53,17 +53,17 @@ func (db AccountSqlite) Update(userId uuid.UUID, account *types.Account) error {
WHERE id = ?
AND user_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, userId)
if err != nil {
log.Error("Error updating account: %v", err)
log.Error("account Update: %v", err)
return types.ErrInternal
}
rows, err := r.RowsAffected()
if err != nil {
log.Error("Error deleting account, getting rows affected: %v", err)
log.Error("account Update: %v", err)
return types.ErrInternal
}
if rows == 0 {
log.Error("Error deleting account, rows affected: %v", rows)
log.Info("account Update: not found")
return ErrNotFound
}
@@ -82,7 +82,7 @@ func (db AccountSqlite) GetAll(userId uuid.UUID) ([]*types.Account, error) {
WHERE user_id = ?
ORDER BY name`, userId)
if err != nil {
log.Error("Could not getAll accounts: %v", err)
log.Error("account GetAll: %v", err)
return nil, types.ErrInternal
}
@@ -101,7 +101,10 @@ func (db AccountSqlite) Get(userId uuid.UUID, id uuid.UUID) (*types.Account, err
WHERE user_id = ?
AND id = ?`, userId, id)
if err != nil {
log.Error("Could not get accounts: %v", err)
if err == sql.ErrNoRows {
return nil, ErrNotFound
}
log.Error("account Get: %v", err)
return nil, types.ErrInternal
}
@@ -112,18 +115,18 @@ func (db AccountSqlite) Delete(userId uuid.UUID, id uuid.UUID) error {
res, err := db.db.Exec("DELETE FROM account WHERE id = ? and user_id = ?", id, userId)
if err != nil {
log.Error("Error deleting account: %v", err)
log.Error("account Delete: %v", err)
return types.ErrInternal
}
rows, err := res.RowsAffected()
if err != nil {
log.Error("Error deleting account, getting rows affected: %v", err)
log.Error("account Delete: %v", err)
return types.ErrInternal
}
if rows == 0 {
log.Error("Error deleting account, rows affected: %v", rows)
log.Info("account Delete: not found")
return ErrNotFound
}

10
db/error.go Normal file
View File

@@ -0,0 +1,10 @@
package db
import (
"errors"
)
var (
ErrNotFound = errors.New("the value does not exist")
ErrAlreadyExists = errors.New("row already exists")
)

View File

@@ -12,11 +12,6 @@ import (
"github.com/jmoiron/sqlx"
)
var (
ErrNotFound = errors.New("the value does not exist")
ErrAlreadyExists = errors.New("row already exists")
)
func RunMigrations(db *sqlx.DB, pathPrefix string) error {
driver, err := sqlite3.WithInstance(db.DB, &sqlite3.Config{})
if err != nil {