JavaScript › Error Handling

What is the Error cause option and what problem does it solve?

Advanced

Answer

The { cause } option on the Error constructor lets you wrap a lower-level error inside a higher-level, more meaningful one while preserving the original error for debugging — avoiding the common trade-off of losing context vs. re-throwing raw low-level errors.
try {
  await fetchUser(id);
} catch (err) {
  throw new Error('Failed to load user profile', { cause: err });
}
Real-world example Wrapping a low-level 'fetch failed' network error with a higher-level, user-facing 'Failed to load profile' error while keeping the original for logs.

Common follow-ups: How do you access the original error from the cause property later?