mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 08:10:28 +00:00
cleanup some inconsistent errors
This commit is contained in:
parent
ad0cf43aca
commit
3a93e336c9
4 changed files with 39 additions and 11 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue