2021-12-02 07:01:50 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
require('../common');
|
|
|
|
|
|
|
|
const { inspect } = require('util');
|
|
|
|
|
|
|
|
class FoobarError extends Error {
|
|
|
|
status = 'Feeling good';
|
|
|
|
}
|
|
|
|
|
|
|
|
const cause1 = new TypeError('Inner error');
|
|
|
|
const cause2 = new FoobarError('Individual message', { cause: cause1 });
|
|
|
|
cause2.extraProperties = 'Yes!';
|
|
|
|
const cause3 = new Error('Stack causes', { cause: cause2 });
|
|
|
|
|
2021-12-06 14:22:30 +01:00
|
|
|
const cause4 = new Error('Number error cause', { cause: 42 });
|
|
|
|
const cause5 = new Error('Object cause', {
|
|
|
|
cause: {
|
|
|
|
message: 'Unique',
|
|
|
|
name: 'Error',
|
|
|
|
stack: 'Error: Unique\n' +
|
2023-01-29 19:15:22 +01:00
|
|
|
' at Module._compile (node:internal/modules/cjs/loader:827:30)',
|
|
|
|
},
|
2021-12-06 14:22:30 +01:00
|
|
|
});
|
2021-12-22 00:46:36 +01:00
|
|
|
const cause6 = new Error('undefined cause', {
|
2023-01-29 19:15:22 +01:00
|
|
|
cause: undefined,
|
2021-12-22 00:46:36 +01:00
|
|
|
});
|
2021-12-06 14:22:30 +01:00
|
|
|
|
|
|
|
console.log(cause4);
|
|
|
|
console.log(cause5);
|
2021-12-22 00:46:36 +01:00
|
|
|
console.log(cause6);
|
2021-12-06 14:22:30 +01:00
|
|
|
|
2021-12-02 07:01:50 +01:00
|
|
|
process.nextTick(() => {
|
|
|
|
const error = new RangeError('New Stack Frames', { cause: cause2 });
|
|
|
|
const error2 = new RangeError('New Stack Frames', { cause: cause3 });
|
|
|
|
|
|
|
|
inspect.defaultOptions.colors = true;
|
|
|
|
|
|
|
|
console.log(inspect(error));
|
|
|
|
console.log(inspect(cause3));
|
|
|
|
console.log(inspect(error2));
|
|
|
|
|
|
|
|
inspect.defaultOptions.colors = false;
|
|
|
|
|
|
|
|
console.log(inspect(error));
|
|
|
|
console.log(inspect(cause3));
|
|
|
|
console.log(inspect(error2));
|
|
|
|
});
|
2023-03-31 18:27:52 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const error = new Error('cause that throws');
|
|
|
|
Reflect.defineProperty(error, 'cause', { get() { throw new Error(); } });
|
|
|
|
console.log(inspect(error));
|
|
|
|
}
|