Skip to content

Commit 0946bab

Browse files
committed
Handle non-string error.message
1 parent c50ced8 commit 0946bab

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/clone.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ module.exports = internals.clone = function (obj, options = {}, _seen = null) {
8989
}
9090

9191
if (baseProto === Types.error &&
92-
(key === 'message' || key === 'stack')) {
92+
key === 'stack') {
9393

9494
continue; // Already a part of the base object
9595
}

test/clone.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,32 @@ describe('clone()', () => {
694694
expect(b).to.equal(a);
695695
expect(b).to.not.shallow.equal(a);
696696
expect(b).to.be.instanceOf(CustomError);
697-
expect(b.stack).to.equal(a.stack); // Explicitly validate the .stack getters
697+
expect(b.stack).to.equal(a.stack); // Explicitly validate the .stack getters
698+
});
699+
700+
it('clones Error with cause', () => {
701+
702+
const a = new TypeError('bad', { cause: new Error('embedded') });
703+
const b = Hoek.clone(a);
704+
705+
expect(b).to.equal(a);
706+
expect(b).to.not.shallow.equal(a);
707+
expect(b).to.be.instanceOf(TypeError);
708+
expect(b.stack).to.equal(a.stack); // Explicitly validate the .stack getters
709+
expect(b.cause.stack).to.equal(a.cause.stack); // Explicitly validate the .stack getters
710+
});
711+
712+
it('clones Error with error message', () => {
713+
714+
const a = new Error();
715+
a.message = new Error('message');
716+
717+
const b = Hoek.clone(a);
718+
719+
//expect(b).to.equal(a); // deepEqual() always compares message using ===
720+
expect(b.message).to.equal(a.message);
721+
expect(b.message).to.not.shallow.equal(a.message);
722+
expect(b.stack).to.equal(a.stack);
698723
});
699724

700725
it('cloned Error handles late stack update', () => {

0 commit comments

Comments
 (0)