Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: publishing state flow #773

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading