feat(account): #49 refactor error handling
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 3m55s
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 3m55s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"spend-sparrow/log"
|
||||
"spend-sparrow/types"
|
||||
|
||||
@@ -100,6 +101,9 @@ func (db AccountSqlite) Get(userId uuid.UUID, id uuid.UUID) (*types.Account, err
|
||||
WHERE user_id = ?
|
||||
AND id = ?`, userId, id)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
log.Error("account Get: %v", err)
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
@@ -2,15 +2,13 @@ package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/log"
|
||||
"spend-sparrow/service"
|
||||
t "spend-sparrow/template/account"
|
||||
"spend-sparrow/types"
|
||||
"spend-sparrow/utils"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -70,7 +68,6 @@ func (h AccountImpl) handleAccountItemComp() http.HandlerFunc {
|
||||
idStr := r.PathValue("id")
|
||||
if idStr == "new" {
|
||||
comp := t.EditAccount(nil)
|
||||
log.Info("Component: %v", comp)
|
||||
h.r.Render(r, w, comp)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ package handler
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func handleError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
@@ -13,9 +13,18 @@ func handleError(w http.ResponseWriter, r *http.Request, err error) {
|
||||
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)
|
||||
utils.TriggerToastWithStatus(w, r, "error", extractErrorMessage(err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
utils.TriggerToastWithStatus(w, r, "error", "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func extractErrorMessage(err error) string {
|
||||
errMsg := err.Error()
|
||||
if errMsg == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.SplitN(errMsg, ":", 2)[0]
|
||||
}
|
||||
|
||||
@@ -37,10 +37,6 @@ func (rr *csrfResponseWriter) Write(data []byte) (int, error) {
|
||||
return rr.ResponseWriter.Write([]byte(dataStr))
|
||||
}
|
||||
|
||||
func (rr *csrfResponseWriter) WriteHeader(statusCode int) {
|
||||
rr.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func CrossSiteRequestForgery(auth service.Auth) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -27,11 +27,13 @@ func Gzip(next http.Handler) http.Handler {
|
||||
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz := gzip.NewWriter(w)
|
||||
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
|
||||
next.ServeHTTP(gzr, r)
|
||||
wrapper := gzipResponseWriter{Writer: gz, ResponseWriter: w}
|
||||
|
||||
next.ServeHTTP(wrapper, r)
|
||||
|
||||
err := gz.Close()
|
||||
if err != nil {
|
||||
if err != nil && err != http.ErrBodyNotAllowed {
|
||||
// if err != nil {
|
||||
log.Error("Gzip: could not close Writer: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -27,8 +27,8 @@ type WrappedWriter struct {
|
||||
}
|
||||
|
||||
func (w *WrappedWriter) WriteHeader(code int) {
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
w.StatusCode = code
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func Log(next http.Handler) http.Handler {
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/log"
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/template"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
|
||||
3
main.go
3
main.go
@@ -134,8 +134,7 @@ func createHandler(d *sqlx.DB, serverSettings *types.Settings) http.Handler {
|
||||
middleware.CacheControl,
|
||||
middleware.CrossSiteRequestForgery(authService),
|
||||
middleware.Authenticate(authService),
|
||||
middleware.Log,
|
||||
// Gzip last, as it compresses the body
|
||||
middleware.Gzip,
|
||||
middleware.Log,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -75,8 +75,8 @@ func (s AccountImpl) Add(user *types.User, name string) (*types.Account, error)
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
|
||||
savedAccount, _ := s.db.Get(user.Id, newId)
|
||||
if savedAccount == nil {
|
||||
savedAccount, err := s.db.Get(user.Id, newId)
|
||||
if err != nil {
|
||||
log.Error("account %v not found after insert: %v", newId, err)
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
@@ -94,11 +94,11 @@ func (s AccountImpl) Update(user *types.User, id uuid.UUID, name string) (*types
|
||||
|
||||
account, err := s.db.Get(user.Id, id)
|
||||
if err != nil {
|
||||
if err == db.ErrNotFound {
|
||||
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
|
||||
}
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
if account == nil {
|
||||
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
|
||||
}
|
||||
|
||||
timestamp := s.clock.Now()
|
||||
account.Name = name
|
||||
@@ -121,11 +121,11 @@ func (s AccountImpl) Get(user *types.User, id uuid.UUID) (*types.Account, error)
|
||||
|
||||
account, err := s.db.Get(user.Id, id)
|
||||
if err != nil {
|
||||
if err == db.ErrNotFound {
|
||||
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
|
||||
}
|
||||
return nil, types.ErrInternal
|
||||
}
|
||||
if account == nil {
|
||||
return nil, fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
|
||||
}
|
||||
|
||||
return account, nil
|
||||
}
|
||||
@@ -151,6 +151,9 @@ func (s AccountImpl) Delete(user *types.User, id uuid.UUID) error {
|
||||
|
||||
account, err := s.db.Get(user.Id, id)
|
||||
if err != nil {
|
||||
if err == db.ErrNotFound {
|
||||
return fmt.Errorf("account %v not found: %w", id, ErrBadRequest)
|
||||
}
|
||||
return types.ErrInternal
|
||||
}
|
||||
|
||||
@@ -168,9 +171,9 @@ func (s AccountImpl) Delete(user *types.User, id uuid.UUID) error {
|
||||
|
||||
func (s AccountImpl) validateAccount(name string) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("empty \"name\": %w", ErrBadRequest)
|
||||
return fmt.Errorf("field \"name\" needs to be set: %w", ErrBadRequest)
|
||||
} else if !safeInputRegex.MatchString(name) {
|
||||
return fmt.Errorf("only letters, dashes and numbers for \"name\": %w", ErrBadRequest)
|
||||
return fmt.Errorf("use only letters, dashes and spaces for \"name\": %w", ErrBadRequest)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -18,36 +18,6 @@ templ Account(accounts []*types.Account) {
|
||||
<div id="account-items" class="my-6 flex flex-col items-center">
|
||||
for _, account := range accounts {
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
@AccountItem(account)
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user