Skip to content

Commit

Permalink
Stop making non-Error properties non-enumerable (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 4, 2025
1 parent 98bbe4e commit 1d8395b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class NonError extends Error {
}
}

const commonProperties = [
const errorProperties = [
{
property: 'name',
enumerable: false,
Expand Down Expand Up @@ -127,14 +127,16 @@ const destroyCircular = ({
to[key] = '[Circular]';
}

for (const {property, enumerable} of commonProperties) {
if (from[property] !== undefined && from[property] !== null) {
Object.defineProperty(to, property, {
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
enumerable: forceEnumerable ? true : enumerable,
configurable: true,
writable: true,
});
if (serialize || to instanceof Error) {
for (const {property, enumerable} of errorProperties) {
if (from[property] !== undefined && from[property] !== null) {
Object.defineProperty(to, property, {
value: isErrorLike(from[property]) ? continueDestroyCircular(from[property]) : from[property],
enumerable: forceEnumerable ? true : enumerable,
configurable: true,
writable: true,
});
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ for (const property of ['cause', 'any']) {
});
}

test('deserialized name, stack, cause and message should not be enumerable, other props should be', t => {
test('deserialized Error class properties should not be enumerable, other props should be', t => {
const object = {
message: 'error message',
stack: 'at <anonymous>:1:13',
Expand All @@ -297,6 +297,10 @@ test('deserialized name, stack, cause and message should not be enumerable, othe
errno: 1,
syscall: 'syscall',
randomProperty: 'random',
notAnError: {
stack: 'Not an error',
cause: 'Wasn’t me',
},
};

const deserialized = deserializeError({...object, ...enumerables});
Expand All @@ -305,6 +309,11 @@ test('deserialized name, stack, cause and message should not be enumerable, othe
Object.keys(enumerables),
Object.keys(deserialized),
);

t.deepEqual(
Object.keys(enumerables.notAnError),
Object.keys(deserialized.notAnError),
);
});

test('should deserialize properties up to `Options.maxDepth` levels deep', t => {
Expand Down

0 comments on commit 1d8395b

Please sign in to comment.