mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 16:20:27 +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/go-chi/chi/v5"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
"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
|
// 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) {
|
func (ctrl *V1Controller) routeUUID(r *http.Request, key string) (uuid.UUID, error) {
|
||||||
ID, err := uuid.Parse(chi.URLParam(r, key))
|
ID, err := uuid.Parse(chi.URLParam(r, key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("failed to parse id")
|
return uuid.Nil, validate.NewInvalidRouteKeyError(key)
|
||||||
return uuid.Nil, validate.ErrInvalidID
|
|
||||||
}
|
}
|
||||||
return ID, nil
|
return ID, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,7 +121,7 @@ func (ctrl *V1Controller) HandleAuthRefresh() server.HandlerFunc {
|
||||||
|
|
||||||
newToken, err := ctrl.svc.User.RenewToken(r.Context(), requestToken)
|
newToken, err := ctrl.svc.User.RenewToken(r.Context(), requestToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return validate.UnauthorizedError()
|
return validate.NewUnauthorizedError()
|
||||||
}
|
}
|
||||||
|
|
||||||
return server.Respond(w, http.StatusOK, newToken)
|
return server.Respond(w, http.StatusOK, newToken)
|
||||||
|
|
|
@ -5,12 +5,38 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func UnauthorizedError() error {
|
type UnauthorizedError struct {
|
||||||
return errors.New("unauthorized")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrInvalidID occurs when an ID is not in a valid form.
|
func (err *UnauthorizedError) Error() string {
|
||||||
var ErrInvalidID = errors.New("ID is not in its proper form")
|
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.
|
// ErrorResponse is the form used for API responses from failures in the API.
|
||||||
type ErrorResponse struct {
|
type ErrorResponse struct {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package mid
|
package mid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/hay-kot/homebox/backend/ent"
|
"github.com/hay-kot/homebox/backend/ent"
|
||||||
|
@ -24,10 +23,15 @@ func Errors(log zerolog.Logger) server.Middleware {
|
||||||
Msg("ERROR occurred")
|
Msg("ERROR occurred")
|
||||||
|
|
||||||
switch {
|
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
|
code = http.StatusBadRequest
|
||||||
resp = server.ErrorResponse{
|
resp = server.ErrorResponse{
|
||||||
Error: "invalid id parameter",
|
Error: err.Error(),
|
||||||
}
|
}
|
||||||
case validate.IsFieldError(err):
|
case validate.IsFieldError(err):
|
||||||
fieldErrors := err.(validate.FieldErrors)
|
fieldErrors := err.(validate.FieldErrors)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue