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 authored and Marsup committed Oct 23, 2024
1 parent a554a8b commit 98652b0
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 98652b0

Please sign in to comment.