Skip to content

Commit

Permalink
Handle non-string error.message
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Nov 28, 2023
1 parent 59455bd commit 377c19f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = internals.clone = function (obj, options = {}, _seen = null) {
}

if (baseProto === Types.error &&
(key === 'message' || key === 'stack')) {
key === 'stack') {

continue; // Already a part of the base object
}
Expand Down
27 changes: 26 additions & 1 deletion test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,32 @@ describe('clone()', () => {
expect(b).to.equal(a);
expect(b).to.not.shallow.equal(a);
expect(b).to.be.instanceOf(CustomError);
expect(b.stack).to.equal(a.stack); // Explicitly validate the .stack getters
expect(b.stack).to.equal(a.stack); // Explicitly validate the .stack getters
});

it('clones Error with cause', () => {

const a = new TypeError('bad', { cause: new Error('embedded') });
const b = Hoek.clone(a);

expect(b).to.equal(a);
expect(b).to.not.shallow.equal(a);
expect(b).to.be.instanceOf(TypeError);
expect(b.stack).to.equal(a.stack); // Explicitly validate the .stack getters
expect(b.cause.stack).to.equal(a.cause.stack); // Explicitly validate the .stack getters
});

it('clones Error with error message', () => {

const a = new Error();
a.message = new Error('message');

const b = Hoek.clone(a);

//expect(b).to.equal(a); // deepEqual() always compares message using ===
expect(b.message).to.equal(a.message);
expect(b.message).to.not.shallow.equal(a.message);
expect(b.stack).to.equal(a.stack);
});

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

0 comments on commit 377c19f

Please sign in to comment.