Concepts

Preserve causal diagnostics

Try the free JavaScript compiler

Wraps an underlying error with context while retaining its cause. The cause can be any value, not necessarily an Error.

Formal Syntax & Structure

javascript syntaxjavascript
new Error(message, { cause: original })

Working Example

Example Snippetjavascript
(async () => {const original=new TypeError("bad input");const wrapped=new Error("load failed",{cause:original});console.log(wrapped.message,wrapped.cause===original,wrapped.cause.name);})()
JavaScript Reference