feat(account): #49 refactor error handling
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m19s
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m19s
This commit is contained in:
@@ -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)
|
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)
|
VALUES (?,?,?,?,?,?,?)`, account.Id, userId, account.Name, 0, 0, account.CreatedAt, account.CreatedBy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error inserting account: %v", err)
|
log.Error("account Insert: %v", err)
|
||||||
return types.ErrInternal
|
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 {
|
func (db AccountSqlite) Update(userId uuid.UUID, account *types.Account) error {
|
||||||
|
|
||||||
log.Info("Updating account: %v", account)
|
|
||||||
r, err := db.db.Exec(`
|
r, err := db.db.Exec(`
|
||||||
UPDATE account
|
UPDATE account
|
||||||
SET
|
SET
|
||||||
@@ -53,17 +52,17 @@ func (db AccountSqlite) Update(userId uuid.UUID, account *types.Account) error {
|
|||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
AND user_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, userId)
|
AND user_id = ?`, account.Name, account.CurrentBalance, account.LastTransaction, account.OinkBalance, account.UpdatedAt, account.UpdatedBy, account.Id, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error updating account: %v", err)
|
log.Error("account Update: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
rows, err := r.RowsAffected()
|
rows, err := r.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error deleting account, getting rows affected: %v", err)
|
log.Error("account Update: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
if rows == 0 {
|
if rows == 0 {
|
||||||
log.Error("Error deleting account, rows affected: %v", rows)
|
log.Info("account Update: not found")
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +81,7 @@ func (db AccountSqlite) GetAll(userId uuid.UUID) ([]*types.Account, error) {
|
|||||||
WHERE user_id = ?
|
WHERE user_id = ?
|
||||||
ORDER BY name`, userId)
|
ORDER BY name`, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not getAll accounts: %v", err)
|
log.Error("account GetAll: %v", err)
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +100,7 @@ func (db AccountSqlite) Get(userId uuid.UUID, id uuid.UUID) (*types.Account, err
|
|||||||
WHERE user_id = ?
|
WHERE user_id = ?
|
||||||
AND id = ?`, userId, id)
|
AND id = ?`, userId, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Could not get accounts: %v", err)
|
log.Error("account Get: %v", err)
|
||||||
return nil, types.ErrInternal
|
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)
|
res, err := db.db.Exec("DELETE FROM account WHERE id = ? and user_id = ?", id, userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error deleting account: %v", err)
|
log.Error("account Delete: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
rows, err := res.RowsAffected()
|
rows, err := res.RowsAffected()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error deleting account, getting rows affected: %v", err)
|
log.Error("account Delete: %v", err)
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
if rows == 0 {
|
if rows == 0 {
|
||||||
log.Error("Error deleting account, rows affected: %v", rows)
|
log.Info("account Delete: not found")
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
db/error.go
Normal file
10
db/error.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("the value does not exist")
|
||||||
|
ErrAlreadyExists = errors.New("row already exists")
|
||||||
|
)
|
||||||
@@ -12,11 +12,6 @@ import (
|
|||||||
"github.com/jmoiron/sqlx"
|
"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 {
|
func RunMigrations(db *sqlx.DB, pathPrefix string) error {
|
||||||
driver, err := sqlite3.WithInstance(db.DB, &sqlite3.Config{})
|
driver, err := sqlite3.WithInstance(db.DB, &sqlite3.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"spend-sparrow/handler/middleware"
|
"spend-sparrow/handler/middleware"
|
||||||
"spend-sparrow/log"
|
"spend-sparrow/log"
|
||||||
"spend-sparrow/service"
|
"spend-sparrow/service"
|
||||||
@@ -49,7 +50,7 @@ func (h AccountImpl) handleAccountPage() http.HandlerFunc {
|
|||||||
|
|
||||||
accounts, err := h.s.GetAll(user)
|
accounts, err := h.s.GetAll(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
handleError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,13 +77,13 @@ func (h AccountImpl) handleAccountItemComp() http.HandlerFunc {
|
|||||||
|
|
||||||
id, err := uuid.Parse(idStr)
|
id, err := uuid.Parse(idStr)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := h.s.Get(user, id)
|
account, err := h.s.Get(user, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
handleError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,18 +114,18 @@ func (h AccountImpl) handleUpdateAccount() http.HandlerFunc {
|
|||||||
if idStr == "new" {
|
if idStr == "new" {
|
||||||
account, err = h.s.Add(user, name)
|
account, err = h.s.Add(user, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
|
handleError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
id, err := uuid.Parse(idStr)
|
id, err := uuid.Parse(idStr)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
account, err = h.s.Update(user, id, name)
|
account, err = h.s.Update(user, id, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
|
handleError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -144,13 +145,13 @@ func (h AccountImpl) handleDeleteAccount() http.HandlerFunc {
|
|||||||
|
|
||||||
id, err := uuid.Parse(r.PathValue("id"))
|
id, err := uuid.Parse(r.PathValue("id"))
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.s.Delete(user, id)
|
err = h.s.Delete(user, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.TriggerToastWithStatus(w, r, "error", err.Error(), http.StatusInternalServerError)
|
handleError(w, r, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
21
handler/error.go
Normal file
21
handler/error.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"spend-sparrow/db"
|
"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 {
|
if user == nil {
|
||||||
return nil, types.ErrInternal
|
return nil, ErrUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
newId, err := service.random.UUID()
|
newId, err := s.random.UUID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.validateAccount(name)
|
err = s.validateAccount(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -64,61 +64,48 @@ func (service AccountImpl) Add(user *types.User, name string) (*types.Account, e
|
|||||||
LastTransaction: nil,
|
LastTransaction: nil,
|
||||||
OinkBalance: 0,
|
OinkBalance: 0,
|
||||||
|
|
||||||
CreatedAt: service.clock.Now(),
|
CreatedAt: s.clock.Now(),
|
||||||
CreatedBy: user.Id,
|
CreatedBy: user.Id,
|
||||||
UpdatedAt: nil,
|
UpdatedAt: nil,
|
||||||
UpdatedBy: nil,
|
UpdatedBy: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.db.Insert(user.Id, account)
|
err = s.db.Insert(user.Id, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
savedAccount, err := service.db.Get(user.Id, newId)
|
savedAccount, _ := s.db.Get(user.Id, newId)
|
||||||
if err != nil {
|
if savedAccount == nil {
|
||||||
if errors.Is(err, db.ErrNotFound) {
|
log.Error("account %v not found after insert: %v", newId, err)
|
||||||
log.Error("Account not found after insert: %v", err)
|
|
||||||
}
|
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
return savedAccount, nil
|
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 {
|
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
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
err := service.validateAccount(name)
|
if account == nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := service.db.Get(user.Id, id)
|
timestamp := s.clock.Now()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
timestamp := service.clock.Now()
|
|
||||||
account.Name = name
|
account.Name = name
|
||||||
account.UpdatedAt = ×tamp
|
account.UpdatedAt = ×tamp
|
||||||
account.UpdatedBy = &user.Id
|
account.UpdatedBy = &user.Id
|
||||||
|
|
||||||
err = service.db.Update(user.Id, account)
|
err = s.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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -126,13 +113,30 @@ func (service AccountImpl) Get(user *types.User, id uuid.UUID) (*types.Account,
|
|||||||
return account, nil
|
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 {
|
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 {
|
if err != nil {
|
||||||
return nil, types.ErrInternal
|
return nil, types.ErrInternal
|
||||||
}
|
}
|
||||||
@@ -140,33 +144,33 @@ func (service AccountImpl) GetAll(user *types.User) ([]*types.Account, error) {
|
|||||||
return accounts, nil
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
if account.UserId != user.Id {
|
if account.UserId != user.Id {
|
||||||
return types.ErrUnauthorized
|
return types.ErrUnauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
err = service.db.Delete(user.Id, account.Id)
|
err = s.db.Delete(user.Id, account.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (service AccountImpl) validateAccount(name string) error {
|
func (s AccountImpl) validateAccount(name string) error {
|
||||||
if name == "" {
|
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) {
|
} 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 {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
8
service/error.go
Normal file
8
service/error.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrBadRequest = errors.New("bad request")
|
||||||
|
ErrUnauthorized = errors.New("unauthorized")
|
||||||
|
)
|
||||||
@@ -45,5 +45,11 @@ func (r *RandomImpl) String(size int) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *RandomImpl) UUID() (uuid.UUID, 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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user