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

Rule proposal: prefer-error-cause #1342

Open
fisker opened this issue Jun 6, 2021 · 3 comments
Open

Rule proposal: prefer-error-cause #1342

fisker opened this issue Jun 6, 2021 · 3 comments

Comments

@fisker
Copy link
Collaborator

fisker commented Jun 6, 2021

When throwing a new error in try/catch or Promise#catch, should add the old error as new error's cause property.

Fail

try {} catch {
	throw new Error('oops');
}
// (I'm not sure about this)
try {} catch (error) {
	error.message = 'oops';
	throw error;
}
promise.catch(() => {
	throw new Error('oops');
})
// Same as above
promise.then(null, () => {
	throw new Error('oops');
})

Pass

try {} catch (error) {
	throw new Error('oops', {cause: error});
}
promise.catch((error) => {
	throw new Error('oops', {cause: error});
})
@sindresorhus
Copy link
Owner

This is now accepted.

@fregante

This comment was marked as off-topic.

@sindresorhus
Copy link
Owner

If anyone wants to work on this, see the initial attempt and feedback in #1793.

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

Successfully merging a pull request may close this issue.

3 participants