feat(account): #49 refactor error handling
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
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"
|
||||
)
|
||||
@@ -49,7 +48,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
|
||||
}
|
||||
|
||||
@@ -69,20 +68,19 @@ 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
|
||||
}
|
||||
|
||||
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 +111,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 +142,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
|
||||
}
|
||||
}
|
||||
|
||||
30
handler/error.go
Normal file
30
handler/error.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/utils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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", 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,11 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/template"
|
||||
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user