-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
Use Object.toJSON()
method to serialize
#38
Conversation
What could be reasons for opting out of this functionality? Apart from the annoyance of a new major version, I'd prefer it to be the default behavior, but it's probably for @sindresorhus to weigh in on. |
readme.md
Outdated
// => {date: '1970-01-01T00:00:00.000Z'} | ||
|
||
const obj = { | ||
foo: 'bar', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider throwing in a rainbow, unicorn, heart or lightning here to increase the consistency, or just to appease @sindresorhus 😉
It could be a breaking change for someone relying on |
Hence the annoyance of a new major version 🙂 |
Yeah, I prefer a major version too. I don't really see the point in adding an option for this even. It can be added later if there's an actual need for it. |
Object.toJSON()
method to serialize
index.js
Outdated
@@ -30,11 +30,29 @@ const commonProperties = [ | |||
{property: 'code', enumerable: true} | |||
]; | |||
|
|||
const destroyCircular = ({from, seen, to_, forceEnumerable}) => { | |||
const called = Symbol('.toJSON called'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const called = Symbol('.toJSON called'); | |
const isCalled = Symbol('.toJSON called'); |
index.d.ts
Outdated
class ErrorWithDate extends Error { | ||
constructor() { | ||
super() | ||
this.date = new Date(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.date = new Date(0) | |
this.date = new Date() |
readme.md
Outdated
@@ -44,6 +44,36 @@ Custom properties are preserved. | |||
Non-enumerable properties are kept non-enumerable (name, message, stack). | |||
Enumerable properties are kept enumerable (all properties besides the non-enumerable ones). | |||
Circular references are handled. | |||
If the input object contains `.toJSON()` then its result will be returned instead of object properties. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the input object contains `.toJSON()` then its result will be returned instead of object properties. | |
If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties. |
const err = new ErrorWithToJSON(); | ||
console.log(serializeError(err)); | ||
// => {date: '1970-01-01T00:00:00.000Z', message: '🦄', name, stack} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
index.d.ts should be in sync with the readme.
readme.md
Outdated
return serializeError(this); | ||
} | ||
} | ||
const err = new ErrorWithToJSON(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const err = new ErrorWithToJSON(); | |
const error = new ErrorWithToJSON(); |
readme.md
Outdated
class ErrorWithDate extends Error { | ||
constructor() { | ||
super() | ||
this.date = new Date(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.date = new Date(0) | |
this.date = new Date(); |
readme.md
Outdated
```js | ||
class ErrorWithDate extends Error { | ||
constructor() { | ||
super() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use semi-colons everywhere.
test.js
Outdated
}; | ||
} | ||
} | ||
const err = new CustomError(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const err = new CustomError(); | |
const error = new CustomError(); |
test.js
Outdated
amount: '$20' | ||
} | ||
}); | ||
t.true(stack !== undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use t.not
test.js
Outdated
name: 'CustomError', | ||
value: 30 | ||
}); | ||
t.true(stack !== undefined); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use t.not
.
index.d.ts
Outdated
@@ -13,6 +13,8 @@ Serialize an `Error` object into a plain object. | |||
Non-error values are passed through. | |||
Custom properties are preserved. | |||
Circular references are handled. | |||
If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties. | |||
It's up on `.toJSON()` implementation to handle circular references and enumerability of the properties. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
Thanks @misozask! |
Fixes #20
Fixes #34