Skip to content

Commit

Permalink
Fix error message and use a more fitting error code
Browse files Browse the repository at this point in the history
  • Loading branch information
christeredvartsen committed Jul 2, 2024
1 parent 52fb676 commit 24bf3ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- +goose Up

-- +goose StatementBegin
CREATE OR REPLACE FUNCTION unique_team_slug() RETURNS trigger AS $unique_team_slug$
BEGIN
IF (SELECT slug from team_slugs WHERE slug = NEW.slug) IS NOT NULL THEN
RAISE 'Team slug is not available: %', NEW.slug
USING ERRCODE = 'unique_violation';
END IF;
RETURN NEW;
END;
$unique_team_slug$ LANGUAGE plpgsql;
-- +goose StatementEnd
5 changes: 5 additions & 0 deletions internal/graph/teams.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/uuid"
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/nais/api/internal/auditlogger"
"github.com/nais/api/internal/auditlogger/audittype"
"github.com/nais/api/internal/auth/authz"
Expand Down Expand Up @@ -47,6 +48,10 @@ func (r *mutationResolver) CreateTeam(ctx context.Context, input model.CreateTea
err = r.database.Transaction(ctx, func(ctx context.Context, dbtx database.Database) error {
team, err = dbtx.CreateTeam(ctx, input.Slug, input.Purpose, input.SlackChannel)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
return apierror.Errorf("Team slug %q is not available.", input.Slug)
}
return err
}

Expand Down

0 comments on commit 24bf3ed

Please sign in to comment.