Files
spend-sparrow/handler/error.go
Tim Wundenberg 7e244ccc07
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m56s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 5m33s
feat(account): #66 remove db interface for accounts
2025-05-16 11:39:22 +02:00

35 lines
935 B
Go

package handler
import (
"errors"
"net/http"
"spend-sparrow/db"
"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
} else if errors.Is(err, db.ErrNotFound) {
utils.TriggerToastWithStatus(w, r, "error", extractErrorMessage(err), http.StatusNotFound)
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]
}