From cbb50860b56784fbf570d7f3473e1b21032631da Mon Sep 17 00:00:00 2001 From: Adnan Maolood Date: Tue, 25 Jan 2022 14:54:17 -0500 Subject: [PATCH] api/graph: Replace gqlErrorf with valid.Errorf --- api/go.mod | 2 +- api/go.sum | 4 ++-- api/graph/resolver.go | 16 ---------------- api/graph/schema.resolvers.go | 15 ++++++++------- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/api/go.mod b/api/go.mod index 0b88aa1..d67d404 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,7 +3,7 @@ module git.sr.ht/~sircmpwn/git.sr.ht/api go 1.14 require ( - git.sr.ht/~sircmpwn/core-go v0.0.0-20220112154231-e28d47cf5957 + git.sr.ht/~sircmpwn/core-go v0.0.0-20220126083025-75dfa2992b49 github.com/99designs/gqlgen v0.14.0 github.com/Masterminds/squirrel v1.4.0 github.com/go-git/go-git/v5 v5.0.0 diff --git a/api/go.sum b/api/go.sum index cf722cc..3f435b3 100644 --- a/api/go.sum +++ b/api/go.sum @@ -31,8 +31,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -git.sr.ht/~sircmpwn/core-go v0.0.0-20220112154231-e28d47cf5957 h1:TjvgAEU7+tsevGSTJU1DMhV7Ue6GnvMWKVIRxLfPcu0= -git.sr.ht/~sircmpwn/core-go v0.0.0-20220112154231-e28d47cf5957/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc= +git.sr.ht/~sircmpwn/core-go v0.0.0-20220126083025-75dfa2992b49 h1:+18ujTlMyXa6Zp1qXz7M4ha2eT8MuItSbeQd9w2LQ7I= +git.sr.ht/~sircmpwn/core-go v0.0.0-20220126083025-75dfa2992b49/go.mod h1:uUqzeO5OLl/nRZfPk0igIAweRZiVwUmu/OGYfjS9fWc= git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3 h1:9WCv5cK67s2SiY/R4DWT/OchEsFnfYDz3lbevKxZ4QI= git.sr.ht/~sircmpwn/dowork v0.0.0-20210820133136-d3970e97def3/go.mod h1:8neHEO3503w/rNtttnR0JFpQgM/GFhaafVwvkPsFIDw= git.sr.ht/~sircmpwn/getopt v0.0.0-20191230200459-23622cc906b3 h1:4wDp4BKF7NQqoh73VXpZsB/t1OEhDpz/zEpmdQfbjDk= diff --git a/api/graph/resolver.go b/api/graph/resolver.go index 8e63e6f..f3352c4 100644 --- a/api/graph/resolver.go +++ b/api/graph/resolver.go @@ -1,12 +1,7 @@ package graph import ( - "context" - "fmt" "regexp" - - "github.com/99designs/gqlgen/graphql" - "github.com/vektah/gqlparser/v2/gqlerror" ) type Resolver struct{} @@ -15,17 +10,6 @@ var ( repoNameRE = regexp.MustCompile(`^[A-Za-z0-9._-]+$`) ) -func gqlErrorf(ctx context.Context, field string, message string, items ...interface{}) *gqlerror.Error { - err := &gqlerror.Error{ - Message: fmt.Sprintf(message, items...), - Path: graphql.GetPath(ctx), - Extensions: map[string]interface{}{ - "field": field, - }, - } - return err -} - var allowedCloneSchemes = map[string]struct{}{ "https": struct{}{}, "http": struct{}{}, diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index ee1c898..a6a4f37 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -26,6 +26,7 @@ import ( "git.sr.ht/~sircmpwn/core-go/database" coremodel "git.sr.ht/~sircmpwn/core-go/model" "git.sr.ht/~sircmpwn/core-go/server" + "git.sr.ht/~sircmpwn/core-go/valid" corewebhooks "git.sr.ht/~sircmpwn/core-go/webhooks" "git.sr.ht/~sircmpwn/git.sr.ht/api/graph/api" "git.sr.ht/~sircmpwn/git.sr.ht/api/graph/model" @@ -141,7 +142,7 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi &repo.Description, &repo.RawVisibility, &repo.UpstreamURL, &repo.Path, &repo.OwnerID); err != nil { if strings.Contains(err.Error(), "duplicate key value violates unique constraint") { - return gqlErrorf(ctx, "name", "A repository with this name already exists.") + return valid.Errorf(ctx, "name", "A repository with this name already exists.") } return err } @@ -152,9 +153,9 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi if err != nil { return err } else if u.Host == "" { - return gqlErrorf(ctx, "cloneUrl", "Cannot use URL without host") + return valid.Errorf(ctx, "cloneUrl", "Cannot use URL without host") } else if _, ok := allowedCloneSchemes[u.Scheme]; !ok { - return gqlErrorf(ctx, "cloneUrl", "Unsupported protocol %q", u.Scheme) + return valid.Errorf(ctx, "cloneUrl", "Unsupported protocol %q", u.Scheme) } origin := config.GetOrigin(conf, "git.sr.ht", true) @@ -168,21 +169,21 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi u.Path = strings.TrimPrefix(u.Path, "/") split := strings.SplitN(u.Path, "/", 2) if len(split) != 2 { - return gqlErrorf(ctx, "cloneUrl", "Invalid clone URL") + return valid.Errorf(ctx, "cloneUrl", "Invalid clone URL") } canonicalName, repoName := split[0], split[1] entity := canonicalName if strings.HasPrefix(entity, "~") { entity = entity[1:] } else { - return gqlErrorf(ctx, "cloneUrl", "Invalid username") + return valid.Errorf(ctx, "cloneUrl", "Invalid username") } repo, err := loaders.ForContext(ctx). RepositoriesByOwnerRepoName.Load([2]string{entity, repoName}) if err != nil { return err } else if repo == nil { - return gqlErrorf(ctx, "cloneUrl", "Repository not found") + return valid.Errorf(ctx, "cloneUrl", "Repository not found") } cloneURL = &repo.Path } @@ -192,7 +193,7 @@ func (r *mutationResolver) CreateRepository(ctx context.Context, name string, vi RecurseSubmodules: git.NoRecurseSubmodules, }) if err != nil { - return gqlErrorf(ctx, "cloneUrl", "%s", err) + return valid.Errorf(ctx, "cloneUrl", "%s", err) } } else { var err error -- 2.38.4