Skip to content

Ignore non-Errors returned by beforeError hooks#833

Merged
sholladay merged 4 commits intosindresorhus:mainfrom
constantly-dev:fix/before-error-return-guard
Mar 2, 2026
Merged

Ignore non-Errors returned by beforeError hooks#833
sholladay merged 4 commits intosindresorhus:mainfrom
constantly-dev:fix/before-error-return-guard

Conversation

@constantly-dev
Copy link
Copy Markdown
Contributor

This is a small follow-up to the discussion in #804 / #508.

#829 resolved the main request by routing timeout (and other errors) through beforeError.
However, at runtime a beforeError hook could still accidentally return a non-Error value (e.g. undefined). Since the hook result was assigned directly, that could lead to throwing a non-Error.

This PR adds a defensive guard so we only overwrite the processed error when the hook returns an Error instance; otherwise we keep the current error.

Context: In #804, maintainers confirmed this approach (“Yes, exactly.”) in the review thread: #804 (comment)

@sholladay sholladay changed the title Fix: beforeError return guard Ignore non-Errors returned by beforeError hooks Mar 2, 2026
Copy link
Copy Markdown
Collaborator

@sholladay sholladay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to have this. Thanks!

@sholladay sholladay merged commit a541fc0 into sindresorhus:main Mar 2, 2026
3 checks passed
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.

2 participants