From 0d05ddd85c6bf24961a13025c7b3390bed1c0dd3 Mon Sep 17 00:00:00 2001 From: martinsaporiti Date: Fri, 6 Sep 2024 07:16:50 -0300 Subject: [PATCH] fix: publishing state flow --- internal/api/identity.go | 9 +++++++++ internal/api/state.go | 7 +++++++ internal/core/services/errors.go | 13 +++++++++++++ internal/core/services/identity.go | 17 +++++++++-------- 4 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 internal/core/services/errors.go diff --git a/internal/api/identity.go b/internal/api/identity.go index f54aa89dc..523b6d2c1 100644 --- a/internal/api/identity.go +++ b/internal/api/identity.go @@ -95,6 +95,15 @@ func (s *Server) CreateIdentity(ctx context.Context, request CreateIdentityReque }, nil } + var customErr *services.PublishingStateError + if errors.As(err, &customErr) { + return CreateIdentity400JSONResponse{ + N400JSONResponse{ + Message: customErr.Error(), + }, + }, nil + } + return nil, err } diff --git a/internal/api/state.go b/internal/api/state.go index 6171386f9..11e0662f0 100644 --- a/internal/api/state.go +++ b/internal/api/state.go @@ -9,6 +9,7 @@ import ( "github.com/iden3/go-iden3-core/v2/w3c" "github.com/polygonid/sh-id-platform/internal/core/ports" + "github.com/polygonid/sh-id-platform/internal/core/services" "github.com/polygonid/sh-id-platform/internal/gateways" "github.com/polygonid/sh-id-platform/internal/log" "github.com/polygonid/sh-id-platform/internal/sqltools" @@ -26,6 +27,12 @@ func (s *Server) PublishIdentityState(ctx context.Context, request PublishIdenti if errors.Is(err, gateways.ErrNoStatesToProcess) || errors.Is(err, gateways.ErrStateIsBeingProcessed) { return PublishIdentityState200JSONResponse{Message: err.Error()}, nil } + + var customErr *services.PublishingStateError + if errors.As(err, &customErr) { + return PublishIdentityState500JSONResponse{N500JSONResponse{Message: customErr.Error()}}, nil + } + return PublishIdentityState500JSONResponse{N500JSONResponse{err.Error()}}, nil } diff --git a/internal/core/services/errors.go b/internal/core/services/errors.go new file mode 100644 index 000000000..006739d63 --- /dev/null +++ b/internal/core/services/errors.go @@ -0,0 +1,13 @@ +package services + +import "fmt" + +// PublishingStateError is a special error type used to signal an error when publishing a state +type PublishingStateError struct { + Message string +} + +// Error satisfies error interface for PublishingStateError +func (e *PublishingStateError) Error() string { + return fmt.Sprintf("Error: %s", e.Message) +} diff --git a/internal/core/services/identity.go b/internal/core/services/identity.go index 4b927e559..b7a7ca15d 100644 --- a/internal/core/services/identity.go +++ b/internal/core/services/identity.go @@ -394,7 +394,10 @@ func (i *identity) UpdateState(ctx context.Context, did w3c.DID) (*domain.Identi if i.ignoreRHSErrors { errIn = nil } else { - return errIn + errIn = &PublishingStateError{ + Message: "error publishing revocation status:" + errIn.Error(), + } + break } } } @@ -583,7 +586,7 @@ func (i *identity) update(ctx context.Context, conn db.Querier, id *w3c.DID, cur var err error claims[j].IdentityState = currentState.State - affected, err := i.claimsRepository.UpdateState(ctx, i.storage.Pgx, &claims[j]) + affected, err := i.claimsRepository.UpdateState(ctx, conn, &claims[j]) if err != nil { return fmt.Errorf("can't update claim: %w", err) } @@ -811,8 +814,10 @@ func (i *identity) createIdentity(ctx context.Context, tx db.Querier, hostURL st }, }) if err != nil { - log.Error(ctx, "publishing state to RHS", "err", err) - return nil, nil, err + log.Error(ctx, "error publishing genesis state", "err", err) + return nil, nil, &PublishingStateError{ + Message: "error publishing genesis state:" + err.Error(), + } } } } @@ -837,10 +842,6 @@ func (i *identity) createEthIdentityFromKeyID(ctx context.Context, mts *domain.I copy(ethAddr[:], address.Bytes()) currentState := core.GenesisFromEthAddress(ethAddr) - if err != nil { - return nil, nil, err - } - didType, err := core.BuildDIDType(didOptions.Method, didOptions.Blockchain, didOptions.Network) if err != nil { return nil, nil, err