Skip to content

Commit

Permalink
fix: publishing state flow
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsaporiti committed Sep 6, 2024
1 parent 7b96d79 commit 0d05ddd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
9 changes: 9 additions & 0 deletions internal/api/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions internal/api/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down
13 changes: 13 additions & 0 deletions internal/core/services/errors.go
Original file line number Diff line number Diff line change
@@ -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)
}
17 changes: 9 additions & 8 deletions internal/core/services/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(),
}
}
}
}
Expand All @@ -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
Expand Down

0 comments on commit 0d05ddd

Please sign in to comment.