From 3a93e336c9d4359e020e1c84cbf094f39d656204 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Fri, 28 Oct 2022 17:56:12 -0800 Subject: [PATCH] cleanup some inconsistent errors --- backend/app/api/handlers/v1/partials.go | 4 +-- backend/app/api/handlers/v1/v1_ctrl_auth.go | 2 +- backend/internal/sys/validate/errors.go | 34 ++++++++++++++++++--- backend/internal/web/mid/errors.go | 10 ++++-- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/backend/app/api/handlers/v1/partials.go b/backend/app/api/handlers/v1/partials.go index 0c025cf..763805f 100644 --- a/backend/app/api/handlers/v1/partials.go +++ b/backend/app/api/handlers/v1/partials.go @@ -6,7 +6,6 @@ import ( "github.com/go-chi/chi/v5" "github.com/google/uuid" "github.com/hay-kot/homebox/backend/internal/sys/validate" - "github.com/rs/zerolog/log" ) // routeID extracts the ID from the request URL. If the ID is not in a valid @@ -22,8 +21,7 @@ func (ctrl *V1Controller) routeID(r *http.Request) (uuid.UUID, error) { func (ctrl *V1Controller) routeUUID(r *http.Request, key string) (uuid.UUID, error) { ID, err := uuid.Parse(chi.URLParam(r, key)) if err != nil { - log.Err(err).Msg("failed to parse id") - return uuid.Nil, validate.ErrInvalidID + return uuid.Nil, validate.NewInvalidRouteKeyError(key) } return ID, nil } diff --git a/backend/app/api/handlers/v1/v1_ctrl_auth.go b/backend/app/api/handlers/v1/v1_ctrl_auth.go index 4b3c0a4..28ee0dc 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_auth.go +++ b/backend/app/api/handlers/v1/v1_ctrl_auth.go @@ -121,7 +121,7 @@ func (ctrl *V1Controller) HandleAuthRefresh() server.HandlerFunc { newToken, err := ctrl.svc.User.RenewToken(r.Context(), requestToken) if err != nil { - return validate.UnauthorizedError() + return validate.NewUnauthorizedError() } return server.Respond(w, http.StatusOK, newToken) diff --git a/backend/internal/sys/validate/errors.go b/backend/internal/sys/validate/errors.go index f2b85eb..d08a448 100644 --- a/backend/internal/sys/validate/errors.go +++ b/backend/internal/sys/validate/errors.go @@ -5,12 +5,38 @@ import ( "errors" ) -func UnauthorizedError() error { - return errors.New("unauthorized") +type UnauthorizedError struct { } -// ErrInvalidID occurs when an ID is not in a valid form. -var ErrInvalidID = errors.New("ID is not in its proper form") +func (err *UnauthorizedError) Error() string { + return "unauthorized" +} + +func IsUnauthorizedError(err error) bool { + var re *UnauthorizedError + return errors.As(err, &re) +} + +func NewUnauthorizedError() error { + return &UnauthorizedError{} +} + +type InvalidRouteKeyError struct { + key string +} + +func (err *InvalidRouteKeyError) Error() string { + return "invalid route key: " + err.key +} + +func NewInvalidRouteKeyError(key string) error { + return &InvalidRouteKeyError{key} +} + +func IsInvalidRouteKeyError(err error) bool { + var re *InvalidRouteKeyError + return errors.As(err, &re) +} // ErrorResponse is the form used for API responses from failures in the API. type ErrorResponse struct { diff --git a/backend/internal/web/mid/errors.go b/backend/internal/web/mid/errors.go index 9762c5f..0802a11 100644 --- a/backend/internal/web/mid/errors.go +++ b/backend/internal/web/mid/errors.go @@ -1,7 +1,6 @@ package mid import ( - "errors" "net/http" "github.com/hay-kot/homebox/backend/ent" @@ -24,10 +23,15 @@ func Errors(log zerolog.Logger) server.Middleware { Msg("ERROR occurred") switch { - case errors.Is(err, validate.ErrInvalidID): + case validate.IsUnauthorizedError(err): + code = http.StatusUnauthorized + resp = server.ErrorResponse{ + Error: "unauthorized", + } + case validate.IsInvalidRouteKeyError(err): code = http.StatusBadRequest resp = server.ErrorResponse{ - Error: "invalid id parameter", + Error: err.Error(), } case validate.IsFieldError(err): fieldErrors := err.(validate.FieldErrors)