feat(account): #49 refactor error handling
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m19s

This commit is contained in:
2025-05-08 21:45:53 +02:00
parent 511c4ca22b
commit bd18453a6e
8 changed files with 118 additions and 74 deletions

View File

@@ -31,7 +31,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 +40,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 +52,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 +81,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 +100,7 @@ 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)
log.Error("account Get: %v", err)
return nil, types.ErrInternal
}
@@ -112,18 +111,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 {

View File

@@ -1,6 +1,7 @@
package handler
import (
"fmt"
"spend-sparrow/handler/middleware"
"spend-sparrow/log"
"spend-sparrow/service"
@@ -49,7 +50,7 @@ func (h AccountImpl) handleAccountPage() http.HandlerFunc {
accounts, err := h.s.GetAll(user)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
handleError(w, r, err)
return
}
@@ -76,13 +77,13 @@ func (h AccountImpl) handleAccountItemComp() http.HandlerFunc {
id, err := uuid.Parse(idStr)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
handleError(w, r, fmt.Errorf("could not parse Id: %w", service.ErrBadRequest))
return
}
account, err := h.s.Get(user, id)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
handleError(w, r, err)
return
}
@@ -113,18 +114,18 @@ func (h AccountImpl) handleUpdateAccount() http.HandlerFunc {
if idStr == "new" {
account, err = h.s.Add(user, name)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
handleError(w, r, err)
return
}
} else {
id, err := uuid.Parse(idStr)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
handleError(w, r, fmt.Errorf("could not parse Id: %w", service.ErrBadRequest))
return
}
account, err = h.s.Update(user, id, name)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
handleError(w, r, err)
return
}
}
@@ -144,13 +145,13 @@ func (h AccountImpl) handleDeleteAccount() http.HandlerFunc {
id, err := uuid.Parse(r.PathValue("id"))
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", "Could not parse Id", http.StatusBadRequest)
handleError(w, r, fmt.Errorf("could not parse Id: %w", service.ErrBadRequest))
return
}
err = h.s.Delete(user, id)
if err != nil {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
handleError(w, r, err)
return
}
}

21
handler/error.go Normal file
View File

@@ -0,0 +1,21 @@
package handler
import (
"errors"
"net/http"
"spend-sparrow/service"
"spend-sparrow/utils"
)
func handleError(w http.ResponseWriter, r *http.Request, err error) {
if errors.Is(err, service.ErrUnauthorized) {
utils.TriggerToastWithStatus(w, r, "error", "You are not autorized to perform this operation.", http.StatusUnauthorized)
return
} else if errors.Is(err, service.ErrBadRequest) {
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusBadRequest)
return
}
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
}

View File

@@ -1,7 +1,7 @@
package service
import (
"errors"
"fmt"
"regexp"
"spend-sparrow/db"
@@ -39,17 +39,17 @@ func NewAccountImpl(db db.Account, random Random, clock Clock, settings *types.S
}
}
func (service AccountImpl) Add(user *types.User, name string) (*types.Account, error) {
func (s AccountImpl) Add(user *types.User, name string) (*types.Account, error) {
if user == nil {
return nil, types.ErrInternal
return nil, ErrUnauthorized
}
newId, err := service.random.UUID()
newId, err := s.random.UUID()
if err != nil {
return nil, types.ErrInternal
}
err = service.validateAccount(name)
err = s.validateAccount(name)
if err != nil {
return nil, err
}
@@ -64,61 +64,48 @@ func (service AccountImpl) Add(user *types.User, name string) (*types.Account, e
LastTransaction: nil,
OinkBalance: 0,
CreatedAt: service.clock.Now(),
CreatedAt: s.clock.Now(),
CreatedBy: user.Id,
UpdatedAt: nil,
UpdatedBy: nil,
}
err = service.db.Insert(user.Id, account)
err = s.db.Insert(user.Id, account)
if err != nil {
return nil, err
return nil, types.ErrInternal
}
savedAccount, err := service.db.Get(user.Id, newId)
if err != nil {
if errors.Is(err, db.ErrNotFound) {
log.Error("Account not found after insert: %v", err)
}
savedAccount, _ := s.db.Get(user.Id, newId)
if savedAccount == nil {
log.Error("account %v not found after insert: %v", newId, err)
return nil, types.ErrInternal
}
return savedAccount, nil
}
func (service AccountImpl) Update(user *types.User, id uuid.UUID, name string) (*types.Account, error) {
func (s AccountImpl) Update(user *types.User, id uuid.UUID, name string) (*types.Account, error) {
if user == nil {
return nil, ErrUnauthorized
}
err := s.validateAccount(name)
if err != nil {
return nil, err
}
account, err := s.db.Get(user.Id, id)
if err != nil {
return nil, types.ErrInternal
}
err := service.validateAccount(name)
if err != nil {
return nil, err
if account == nil {
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
}
account, err := service.db.Get(user.Id, id)
if err != nil {
return nil, err
}
timestamp := service.clock.Now()
timestamp := s.clock.Now()
account.Name = name
account.UpdatedAt = &timestamp
account.UpdatedBy = &user.Id
err = service.db.Update(user.Id, account)
if err != nil {
return nil, err
}
return account, nil
}
func (service AccountImpl) Get(user *types.User, id uuid.UUID) (*types.Account, error) {
if user == nil {
return nil, types.ErrInternal
}
account, err := service.db.Get(user.Id, id)
err = s.db.Update(user.Id, account)
if err != nil {
return nil, types.ErrInternal
}
@@ -126,13 +113,30 @@ func (service AccountImpl) Get(user *types.User, id uuid.UUID) (*types.Account,
return account, nil
}
func (service AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
func (s AccountImpl) Get(user *types.User, id uuid.UUID) (*types.Account, error) {
if user == nil {
return nil, types.ErrInternal
return nil, ErrUnauthorized
}
accounts, err := service.db.GetAll(user.Id)
account, err := s.db.Get(user.Id, id)
if err != nil {
return nil, types.ErrInternal
}
if account == nil {
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
}
return account, nil
}
func (s AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
if user == nil {
return nil, ErrUnauthorized
}
accounts, err := s.db.GetAll(user.Id)
if err != nil {
return nil, types.ErrInternal
}
@@ -140,33 +144,33 @@ func (service AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
return accounts, nil
}
func (service AccountImpl) Delete(user *types.User, id uuid.UUID) error {
func (s AccountImpl) Delete(user *types.User, id uuid.UUID) error {
if user == nil {
return types.ErrInternal
return ErrUnauthorized
}
account, err := service.db.Get(user.Id, id)
account, err := s.db.Get(user.Id, id)
if err != nil {
return err
return types.ErrInternal
}
if account.UserId != user.Id {
return types.ErrUnauthorized
}
err = service.db.Delete(user.Id, account.Id)
err = s.db.Delete(user.Id, account.Id)
if err != nil {
return err
return types.ErrInternal
}
return nil
}
func (service AccountImpl) validateAccount(name string) error {
func (s AccountImpl) validateAccount(name string) error {
if name == "" {
return errors.New("please enter a value for the \"name\" field")
return fmt.Errorf("empty \"name\": %w", ErrBadRequest)
} else if !safeInputRegex.MatchString(name) {
return errors.New("please use only letters, dashes or numbers for \"name\"")
return fmt.Errorf("only letters, dashes and numbers for \"name\": %w", ErrBadRequest)
} else {
return nil
}

8
service/error.go Normal file
View File

@@ -0,0 +1,8 @@
package service
import "errors"
var (
ErrBadRequest = errors.New("bad request")
ErrUnauthorized = errors.New("unauthorized")
)

View File

@@ -45,5 +45,11 @@ func (r *RandomImpl) String(size int) (string, error) {
}
func (r *RandomImpl) UUID() (uuid.UUID, error) {
return uuid.NewRandom()
id, err := uuid.NewRandom()
if err != nil {
log.Error("Error generating random UUID: %v", err)
return uuid.Nil, types.ErrInternal
}
return id, nil
}