Skip to content

fix(hooks): wrap errors to preserve sentinel values#760

Open
victoriacheng15 wants to merge 1 commit into
cucumber:mainfrom
victoriacheng15:fix/hook-error-wrapping
Open

fix(hooks): wrap errors to preserve sentinel values#760
victoriacheng15 wants to merge 1 commit into
cucumber:mainfrom
victoriacheng15:fix/hook-error-wrapping

Conversation

@victoriacheng15

@victoriacheng15 victoriacheng15 commented Jul 10, 2026

Copy link
Copy Markdown

🤔 What's changed?

This PR fixes hook error wrapping to preserve the types of all merged errors.

Specifically:

  • Introduces a custom joinedError type and mergeErrors helper at the end of suite.go.
  • Updates runBeforeStepHooks, runAfterStepHooks, runBeforeScenarioHooks, and runAfterScenarioHooks in suite.go to use mergeErrors instead of fmt.Errorf("%v, %w").
  • This ensures that all errors returned from hook handlers are correctly wrapped, making them fully detectable by errors.Is and errors.As.

Backward Compatibility:

  • Go 1.18 - 1.19: Supported via the custom Is and As methods on joinedError, which have been natively supported by the standard errors package since Go 1.13.
  • Go 1.20+: Supported via the Unwrap() []error method, allowing modern Go runtimes and static analysis tools to natively unwrap the error slice.
  • Error Messages: Preserves the original %v, %v string formatting, ensuring no breaking changes to logs or output formatting.

⚡️ What's your motivation?

Fixes #732

Currently, when a step fails and a hook also returns an error (such as a skip), they are merged using fmt.Errorf("%v, %w"). This prevents errors.Is and errors.As from detecting the hook error type because it is formatted with %v instead of %w. As a result, the runner fails to recognize sentinel errors like ErrSkip returned from hooks when a step fails.

🏷️ What kind of change is this?

  • 🐛 Bug fix (non-breaking change which fixes a defect)

♻️ Anything particular you want feedback on?

Feedback on the joinedError implementation for Go versions < 1.20 (which do not support multiple %w verbs).

📋 Checklist:

  • I agree to respect and uphold the Cucumber Community Code of Conduct
  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

This text was originally generated from a template, then edited by hand. You can modify the template here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting error in after step hook as godog error not recognized

1 participant