Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error cloning to fix node v21 issue #390

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ module.exports = internals.clone = function (obj, options = {}, _seen = null) {
continue;
}

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

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

const descriptor = Object.getOwnPropertyDescriptor(obj, key);
if (descriptor) {
if (descriptor.get ||
Expand Down Expand Up @@ -162,6 +168,14 @@ internals.base = function (obj, baseProto, options) {

return newObj;
}
else if (baseProto === Types.error) {
const err = structuredClone(obj); // Needed to copy internal stack state
if (proto !== baseProto) {
Object.setPrototypeOf(err, proto); // Fix prototype
}

return err;
}

if (internals.needsProtoHack.has(baseProto)) {
const newObj = new proto.constructor();
Expand Down
53 changes: 53 additions & 0 deletions test/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,59 @@ describe('clone()', () => {
expect(b.get(nestedObj)).to.equal(a.get(nestedObj));
});

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

class CustomError extends Error {
name = 'CustomError';
}

const a = new CustomError('bad');
a.test = Symbol('test');

const b = Hoek.clone(a);

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
});

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', () => {

const a = new Error('bad');
const b = Hoek.clone(a);

a.stack = 'late update';

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

it('ignores symbols', () => {

const sym = Symbol();
Expand Down