From 86286ebe61c5bd0f88689d5d6155f09cc4879821 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 25 Mar 2026 16:45:15 +0200 Subject: refactor: Add error wrapping with %w where appropriate - Updated operator registration in rpn/operations.go: - registerStandardOperator now wraps handler errors with operator name context - registerCommandOperator now wraps handler errors with operator name context - registerHyperOperator now wraps handler errors with operator name context - Updated Parse() in calculator/calculator.go: - Now wraps registry.parse() errors with context about the input - Returns a more descriptive error when parsing fails All tests pass and the binary builds and runs correctly. --- internal/calculator/calculator.go | 4 ++-- internal/rpn/operations.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/calculator/calculator.go b/internal/calculator/calculator.go index f953df7..0212279 100644 --- a/internal/calculator/calculator.go +++ b/internal/calculator/calculator.go @@ -97,10 +97,10 @@ func Parse(input string) (string, error) { return calc.Format(), nil } if err != nil { - return "", err + return "", fmt.Errorf("calculator: unable to parse input %q: %w", input, err) } - return "", fmt.Errorf("calculator: unable to parse input %q. See usage for examples", input) + return "", fmt.Errorf("calculator: unable to parse input %q: unknown error", input) } // ParseCalculation parses a percentage calculation input string and returns the Calculation object. diff --git a/internal/rpn/operations.go b/internal/rpn/operations.go index d749720..aeccf68 100644 --- a/internal/rpn/operations.go +++ b/internal/rpn/operations.go @@ -136,7 +136,7 @@ func NewOperatorRegistry(op Operator) *OperatorRegistry { func (r *OperatorRegistry) registerStandardOperator(name string, handler func(*Stack) error) { r.standardOperators[name] = func(stack *Stack) (string, bool, error) { if err := handler(stack); err != nil { - return "", false, err + return "", false, fmt.Errorf("%s: %w", name, err) } return "", true, nil } @@ -147,7 +147,7 @@ func (r *OperatorRegistry) registerCommandOperator(name string, handler func(*St r.standardOperators[name] = func(stack *Stack) (string, bool, error) { result, err := handler(stack) if err != nil { - return "", false, err + return "", false, fmt.Errorf("%s: %w", name, err) } return result, true, nil } @@ -157,7 +157,7 @@ func (r *OperatorRegistry) registerCommandOperator(name string, handler func(*St func (r *OperatorRegistry) registerHyperOperator(name string, handler func(*Stack) error) { r.hyperOperators[name] = func(stack *Stack) (string, bool, error) { if err := handler(stack); err != nil { - return "", false, err + return "", false, fmt.Errorf("%s: %w", name, err) } return "", true, nil } -- cgit v1.2.3