diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-25 16:45:15 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-25 16:45:15 +0200 |
| commit | 86286ebe61c5bd0f88689d5d6155f09cc4879821 (patch) | |
| tree | 87e33c2be0f4c0ad0e3245195d7525dcfced05b6 /internal | |
| parent | 6553891403e221134028dbdec3ce3101bf97cdfc (diff) | |
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.
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/calculator/calculator.go | 4 | ||||
| -rw-r--r-- | internal/rpn/operations.go | 6 |
2 files changed, 5 insertions, 5 deletions
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 } |
