Skip to content

Commit

Permalink
Fix cloning Util.inherit() subclassed errors. Backports #401
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil authored and Marsup committed Nov 14, 2024
1 parent 9b91eee commit 5870f1f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,12 @@ internals.base = function (obj, baseProto, options) {
}
// Can only be covered in node 21+
/* $lab:coverage:off$ */
else if (baseProto === Types.error && internals.structuredCloneExists) {
const err = structuredClone(obj); // Needed to copy internal stack state
else if (baseProto === Types.error && internals.structuredCloneExists &&
(proto === baseProto || Error.isPrototypeOf(proto.constructor))) { // Don't match Util.inherit() subclassed errors

const err = structuredClone(obj); // Needed to copy internal stack state
if (Object.getPrototypeOf(err) !== proto) {
Object.setPrototypeOf(err, proto); // Fix prototype
Object.setPrototypeOf(err, proto); // Fix prototype
}

return err;
Expand Down
29 changes: 29 additions & 0 deletions test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,35 @@ describe('clone()', () => {
expect(b.stack).to.equal(a.stack);
});

it('clones Error with function property', () => {

const a = new Error('hello');
a.fun = new Function();

const b = Hoek.clone(a);

expect(b).to.equal(a);
expect(b.stack).to.equal(a.stack);
});

it('clones legacy extended Error with function property', () => {

const CustomError = function () {

Error.call(this);
};

Object.setPrototypeOf(CustomError.prototype, Error.prototype);

const a = new CustomError('hello');
a.fun = new Function();

const b = Hoek.clone(a);

expect(b).to.equal(a);
expect(b.stack).to.equal(a.stack);
});

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

const a = new Error('bad');
Expand Down

0 comments on commit 5870f1f

Please sign in to comment.