Skip to content

Commit

Permalink
chore: meaningful duplicated imported schema error
Browse files Browse the repository at this point in the history
  • Loading branch information
javip97 committed Nov 28, 2023
1 parent 7c313dc commit fa9e2f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions internal/repositories/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ import (
"github.com/google/uuid"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"

"github.com/polygonid/sh-id-platform/internal/core/domain"
"github.com/polygonid/sh-id-platform/internal/db"
)

// ErrSchemaDoesNotExist claim does not exist
var ErrSchemaDoesNotExist = errors.New("schema does not exist")
const duplicatedEntryPGCode = "23505"

var (
ErrSchemaDoesNotExist = errors.New("schema does not exist") // ErrSchemaDoesNotExist schema does not exist
ErrSchemaDuplicated = errors.New("schema already imported") // ErrSchemaDuplicated schema duplicated
)

type dbSchema struct {
ID uuid.UUID
Expand Down Expand Up @@ -61,7 +65,16 @@ func (r *schema) Save(ctx context.Context, s *domain.Schema) error {
s.Version,
s.Title,
s.Description)
return err
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.Code == duplicatedEntryPGCode {
return ErrSchemaDuplicated
}

return err
}

return nil
}

func (r *schema) toFullTextSearchDocument(sType string, attrs domain.SchemaWords) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/repositories/tests/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestCreateSchema(t *testing.T) {
}

require.NoError(t, store.Save(ctx, schema1))
assert.Error(t, store.Save(ctx, schema1), "cannot have duplicated schemas with the same version for the same issuer and type")
assert.ErrorIs(t, repositories.ErrSchemaDuplicated, store.Save(ctx, schema1), "cannot have duplicated schemas with the same version for the same issuer and type")

schema2 := schema1
schema2.Version = uuid.NewString()
Expand Down

0 comments on commit fa9e2f7

Please sign in to comment.