feat(account): #49 refactor error handling
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 3m55s

This commit is contained in:
2025-05-08 22:58:14 +02:00
parent bd18453a6e
commit fecf53fd20
10 changed files with 39 additions and 58 deletions

View File

@@ -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
}

View File

@@ -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]
}

View File

@@ -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) {

View File

@@ -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)
}
})

View File

@@ -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 {

View File

@@ -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"
)