From b3ec44cd70f14a1e6c58f6adbc349495c29333fc Mon Sep 17 00:00:00 2001 From: zepatrik Date: Fri, 9 Aug 2024 16:07:50 +0200 Subject: [PATCH] chore: extract error to variable --- persistence/sql/identity/persister_identity.go | 2 +- persistence/sql/persister_courier.go | 3 ++- persistence/sql/persister_session.go | 3 ++- x/err.go | 15 +++++++++------ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/persistence/sql/identity/persister_identity.go b/persistence/sql/identity/persister_identity.go index c38347e80dfa..0f415e5c1add 100644 --- a/persistence/sql/identity/persister_identity.go +++ b/persistence/sql/identity/persister_identity.go @@ -800,7 +800,7 @@ func (p *IdentityPersister) ListIdentities(ctx context.Context, params identity. defer otelx.End(span, &err) if _, err := uuid.FromString(paginator.Token().Encode()); err != nil { - return nil, nil, errors.WithStack(herodot.ErrBadRequest.WithReason("The page token is invalid, do not craft your own page tokens")) + return nil, nil, errors.WithStack(x.PageTokenInvalid) } nid := p.NetworkID(ctx) diff --git a/persistence/sql/persister_courier.go b/persistence/sql/persister_courier.go index 427e19b6a080..c63728869bc0 100644 --- a/persistence/sql/persister_courier.go +++ b/persistence/sql/persister_courier.go @@ -20,6 +20,7 @@ import ( "github.com/ory/kratos/courier" "github.com/ory/kratos/persistence/sql/update" + "github.com/ory/kratos/x" ) var _ courier.Persister = new(Persister) @@ -58,7 +59,7 @@ func (p *Persister) ListMessages(ctx context.Context, filter courier.ListCourier paginator := keysetpagination.GetPaginator(opts...) if _, err := uuid.FromString(paginator.Token().Encode()); err != nil { - return nil, 0, nil, errors.WithStack(herodot.ErrBadRequest.WithReason("The page token is invalid, do not craft your own page tokens")) + return nil, 0, nil, errors.WithStack(x.PageTokenInvalid) } messages := make([]courier.Message, paginator.Size()) diff --git a/persistence/sql/persister_session.go b/persistence/sql/persister_session.go index 1f4c2ab549c0..10bcadffa4ba 100644 --- a/persistence/sql/persister_session.go +++ b/persistence/sql/persister_session.go @@ -20,6 +20,7 @@ import ( "github.com/ory/kratos/identity" "github.com/ory/kratos/session" + "github.com/ory/kratos/x" "github.com/ory/kratos/x/events" "github.com/ory/x/otelx" "github.com/ory/x/pagination/keysetpagination" @@ -83,7 +84,7 @@ func (p *Persister) ListSessions(ctx context.Context, active *bool, paginatorOpt paginator := keysetpagination.GetPaginator(paginatorOpts...) if _, err := uuid.FromString(paginator.Token().Encode()); err != nil { - return nil, 0, nil, errors.WithStack(herodot.ErrBadRequest.WithReason("The page token is invalid, do not craft your own page tokens")) + return nil, 0, nil, errors.WithStack(x.PageTokenInvalid) } if err := p.Transaction(ctx, func(ctx context.Context, c *pop.Connection) error { diff --git a/x/err.go b/x/err.go index 2a42b3eb540b..5b3868734cca 100644 --- a/x/err.go +++ b/x/err.go @@ -10,12 +10,15 @@ import ( "github.com/ory/herodot" ) -var PseudoPanic = herodot.DefaultError{ - StatusField: http.StatusText(http.StatusInternalServerError), - ErrorField: "Code Bug Detected", - ReasonField: "The code ended up at a place where it should not have. Please report this as an issue at https://github.com/ory/kratos", - CodeField: http.StatusInternalServerError, -} +var ( + PseudoPanic = herodot.DefaultError{ + StatusField: http.StatusText(http.StatusInternalServerError), + ErrorField: "Code Bug Detected", + ReasonField: "The code ended up at a place where it should not have. Please report this as an issue at https://github.com/ory/kratos", + CodeField: http.StatusInternalServerError, + } + PageTokenInvalid = herodot.ErrBadRequest.WithReason("The page token is invalid, do not craft your own page tokens") +) func RecoverStatusCode(err error, fallback int) int { var sc herodot.StatusCodeCarrier