Skip to content

Stops making non-Error properties non-enumerable #102

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

Merged
merged 4 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading