Skip to content

Commit

Permalink
Error handling improvement for parser and reconciler packages (#443)
Browse files Browse the repository at this point in the history
* feat: error handling for parser and reconciler

Signed-off-by: Jingfu Wang <[email protected]>

* fix: make gen

Signed-off-by: Jingfu Wang <[email protected]>

Signed-off-by: Jingfu Wang <[email protected]>
  • Loading branch information
GeekArthur authored Sep 1, 2022
1 parent 64419e3 commit 1ffb630
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 125 deletions.
21 changes: 17 additions & 4 deletions parser/balance_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ func (p *Parser) skipOperation(op *types.Operation) (bool, error) {
successful, err := p.Asserter.OperationSuccessful(op)
if err != nil {
// Should only occur if responses not validated
return false, err
return false, fmt.Errorf(
"failed to check the status of operation %s: %w",
types.PrintStruct(op),
err,
)
}

if !successful {
Expand Down Expand Up @@ -82,7 +86,11 @@ func (p *Parser) BalanceChanges(
for _, op := range tx.Operations {
skip, err := p.skipOperation(op)
if err != nil {
return nil, err
return nil, fmt.Errorf(
"failed to skip operation %s: %w",
types.PrintStruct(op),
err,
)
}
if skip {
continue
Expand All @@ -96,7 +104,7 @@ func (p *Parser) BalanceChanges(
if blockRemoved {
negatedValue, err := types.NegateValue(amountValue)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to flip the sign of %s: %w", amountValue, err)
}
amountValue = negatedValue
}
Expand All @@ -121,7 +129,12 @@ func (p *Parser) BalanceChanges(

newDifference, err := types.AddValues(val.Difference, amountValue)
if err != nil {
return nil, err
return nil, fmt.Errorf(
"failed to add %s and %s: %w",
val.Difference,
amountValue,
err,
)
}
val.Difference = newDifference
balanceChanges[key] = val
Expand Down
26 changes: 15 additions & 11 deletions parser/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ import (
func ExpectedOperation(intent *types.Operation, observed *types.Operation) error {
if types.Hash(intent.Account) != types.Hash(observed.Account) {
return fmt.Errorf(
"%w: expected %s but got %s",
ErrExpectedOperationAccountMismatch,
"expected operation account identifier %s but got %s: %w",
types.PrettyPrintStruct(intent.Account),
types.PrettyPrintStruct(observed.Account),
ErrExpectedOperationAccountMismatch,
)
}

if types.Hash(intent.Amount) != types.Hash(observed.Amount) {
return fmt.Errorf(
"%w: expected %s but got %s",
ErrExpectedOperationAmountMismatch,
"expected operation amount %s but got %s: %w",
types.PrettyPrintStruct(intent.Amount),
types.PrettyPrintStruct(observed.Amount),
ErrExpectedOperationAmountMismatch,
)
}

if intent.Type != observed.Type {
return fmt.Errorf(
"%w: expected %s but got %s",
ErrExpectedOperationTypeMismatch,
"expected operation type %s but got %s: %w",
intent.Type,
observed.Type,
ErrExpectedOperationTypeMismatch,
)
}

Expand Down Expand Up @@ -89,7 +89,11 @@ func (p *Parser) ExpectedOperations(
if confirmSuccess {
obsSuccess, err := p.Asserter.OperationSuccessful(obs)
if err != nil {
return fmt.Errorf("%w: unable to check operation success", err)
return fmt.Errorf(
"failed to check the status of operation %s: %w",
types.PrintStruct(obs),
err,
)
}

if !obsSuccess {
Expand Down Expand Up @@ -168,9 +172,9 @@ func ExpectedSigners(intent []*types.SigningPayload, observed []*types.AccountId
hash := types.Hash(payload.AccountIdentifier)
if _, exists := seenSigners[hash]; !exists {
return fmt.Errorf(
"%w: %s",
ErrExpectedSignerMissing,
"payload account identifier %s is invalid: %w",
types.PrintStruct(payload.AccountIdentifier),
ErrExpectedSignerMissing,
)
}
}
Expand All @@ -179,9 +183,9 @@ func ExpectedSigners(intent []*types.SigningPayload, observed []*types.AccountId
// were not expected.
if len(unmatched) != 0 {
return fmt.Errorf(
"%w: %s",
"unmatched signers are %s: %w",
types.PrintStruct(unmatched),
ErrExpectedSignerUnexpectedSigner,
types.PrettyPrintStruct(unmatched),
)
}

Expand Down
Loading

0 comments on commit 1ffb630

Please sign in to comment.