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

Error handling improvement for parser and reconciler packages #443

Merged
merged 2 commits into from
Sep 1, 2022
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
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