Sub topic 11
How the cause Property Is Defined
This chapter assumes you already know how to pass { cause } to the Error constructor and how wrapping errors at layer boundaries works. Here we look at the exact rules the constructor uses when it creates the cause property. These rules matter when you log, copy, or inspect errors.
The Rule the Constructor Follows
The ECMAScript specification gives every error constructor a step called InstallErrorCause. It works like this:
- If the second argument is not an object, nothing happens.
- If that object has a
causeproperty (own or inherited), the constructor copies its value onto the new error. - The copied property is an own data property that is writable, configurable, and non-enumerable.
Seeing the Rules in Action
const withCause = new Error('outer', { cause: 'low-level detail' });
console.log(Object.getOwnPropertyDescriptor(withCause, 'cause'));
// { value: 'low-level detail', writable: true, enumerable: false, configurable: true }
console.log(Object.keys(withCause)); // []
const withoutCause = new Error('plain');
console.log('cause' in withoutCause); // false
const explicitUndefined = new Error('x', { cause: undefined });
console.log('cause' in explicitUndefined); // trueThis example shows three facts:
- Any value can be a cause. A string, a number, a plain object, or
nullis accepted. Nothing checks that it is anError. Code that readserr.cause.messageshould check the type first. - Absent and undefined are different. Without the key,
'cause' in errisfalse. With{ cause: undefined }, the property exists and holdsundefined. Use'cause' in errorObject.hasOwn(err, 'cause')when the difference matters. Error.prototypehas nocause. The property only exists on instances that were given one.
Why Non-Enumerable Matters
message and stack are non-enumerable too. The spec made cause match them so that errors look the same as before when you enumerate them. The cost is that several common tools skip cause without any warning:
| Operation | Includes `cause`? |
|---|---|
| `Object.keys(err)` | No |
| `{ ...err }` spread | No |
| `JSON.stringify(err)` | No |
| `Object.getOwnPropertyNames(err)` | Yes |
| `console.log(err)` in Node.js | Yes |
Edge Cases Worth Knowing
// Inherited keys count, because the check uses `in`, not own-property lookup.
const defaults = { cause: 'inherited' };
const opts = Object.create(defaults);
console.log(new Error('a', opts).cause); // 'inherited'
// Non-object options are ignored.
console.log('cause' in new Error('b', 'not an object')); // false
// AggregateError takes options as its third argument.
const agg = new AggregateError([], 'none', { cause: 'c' });
console.log(agg.cause); // 'c'Custom subclasses only get this behavior if they forward options to super. If a constructor calls super(message) and drops options, the cause is lost.
Exercise
A logger prints JSON.stringify({ ...err }) and never shows the cause, even though the error was created with new Error('x', { cause: inner }). Why, and what is a quick way to include it?
Show answer
The constructor defines cause as non-enumerable, so both spread and JSON.stringify skip it. Copy it explicitly, for example JSON.stringify({ ...err, message: err.message, cause: String(err.cause) }), or use a serializer that reads cause directly.
Exercise
What do 'cause' in new Error('a', {}) and 'cause' in new Error('a', { cause: undefined }) return?
Show answer
The first returns false because the options object has no cause key. The second returns true because the key exists, so the property is created with the value undefined.