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

Add useToJSON option #71

Merged
merged 5 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export interface Options {
```
*/
readonly maxDepth?: number;

/**
Indicate whether to use a `.toJSON()` method if encountered in the object. This is useful when a custom error implements its own serialization logic via `.toJSON()` but you prefer not using it.
fregante marked this conversation as resolved.
Show resolved Hide resolved

@default true

fregante marked this conversation as resolved.
Show resolved Hide resolved
*/
readonly useToJSON?: boolean;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ const toJSON = from => {
return json;
};

// eslint-disable-next-line complexity
const destroyCircular = ({
from,
seen,
to_,
forceEnumerable,
maxDepth,
depth,
useToJSON,
}) => {
const to = to_ || (Array.isArray(from) ? [] : {});

Expand All @@ -62,7 +64,7 @@ const destroyCircular = ({
return to;
}

if (typeof from.toJSON === 'function' && from[toJsonWasCalled] !== true) {
if (useToJSON && typeof from.toJSON === 'function' && from[toJsonWasCalled] !== true) {
return toJSON(from);
}

Expand Down Expand Up @@ -97,6 +99,7 @@ const destroyCircular = ({
forceEnumerable,
maxDepth,
depth,
useToJSON,
});
continue;
}
Expand All @@ -119,7 +122,10 @@ const destroyCircular = ({
};

export function serializeError(value, options = {}) {
const {maxDepth = Number.POSITIVE_INFINITY} = options;
const {
maxDepth = Number.POSITIVE_INFINITY,
useToJSON = true,
} = options;

if (typeof value === 'object' && value !== null) {
return destroyCircular({
Expand All @@ -128,6 +134,7 @@ export function serializeError(value, options = {}) {
forceEnumerable: true,
maxDepth,
depth: 0,
useToJSON,
});
}

Expand Down
11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,15 @@ const error = new Error('🦄');
error.one = {two: {three: {}}};

console.log(serializeError(error, {maxDepth: 1}));
//=> {name: 'Error', message: '', one: {}}
//=> {name: 'Error', message: '🦄', one: {}}

console.log(serializeError(error, {maxDepth: 2}));
//=> {name: 'Error', message: '', one: { two: {}}}
//=> {name: 'Error', message: '🦄', one: { two: {}}}
```

#### useToJSON

Type: `boolean`\
Default: `true`

Indicate whether to use a `.toJSON()` method if encountered in the object. This is useful when a custom error implements [its own serialization logic via `.toJSON()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#tojson_behavior) but you prefer not using it.
25 changes: 25 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,31 @@ test('should serialize custom error with `.toJSON` defined with `serializeError`
t.not(stack, undefined);
});

test('should ignore `.toJSON` methods if set in the options', t => {
class CustomError extends Error {
constructor() {
super('foo');
this.name = this.constructor.name;
this.value = 10;
}

toJSON() {
return {
message: this.message,
amount: `$${this.value}`,
};
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too cramped :P

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copy-pasted your code 😎

C-658VsXoAo3ovC

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who reviewed and merged #38? 😜

image

const error = new CustomError();
const serialized = serializeError(error, {useToJSON: false});
t.like(serialized, {
name: 'CustomError',
message: 'foo',
value: 10,
});
t.truthy(serialized.stack);
});

test('should serialize properties up to `Options.maxDepth` levels deep', t => {
const error = new Error('errorMessage');
error.one = {two: {three: {}}};
Expand Down