javascript / intermediate
Snippet
Chaining Context with Error Cause
The 'cause' property in the Error constructor allows you to wrap a low-level error in a more descriptive high-level error without losing the original stack trace and context.
snippet.js
1
2
3
4
5
6
7
8
9
function processOrder() {try {throw new Error('Database connection lost');} catch (err) {throw new Error('Order failed', { cause: err });}}// Access via: error.cause
Breakdown
1
{ cause: err }
Attaches the original caught error as the reason for the new error.