{"name":"serialize-error","version":"7.0.0","description":"Serialize/deserialize an error into a plain object","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/serialize-error.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"engines":{"node":">=10"},"scripts":{"test":"xo && ava && tsd"},"keywords":["error","serialize","stringify","object","convert","process","send","deserialize"],"dependencies":{"type-fest":"^0.13.1"},"devDependencies":{"ava":"^2.4.0","tsd":"^0.11.0","xo":"^0.30.0"},"gitHead":"7072820d53464f13b17c0d2f35426ec2481237ac","bugs":{"url":"https://github.com/sindresorhus/serialize-error/issues"},"homepage":"https://github.com/sindresorhus/serialize-error#readme","_id":"serialize-error@7.0.0","_nodeVersion":"10.20.1","_npmVersion":"6.14.5","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"dist":{"integrity":"sha512-xDtzsA4FzjMMfA6bhhbnkXPYS/CA34+TjO9YKf4nas10Akb3HWYfr6nKwHYxV0xO5Qr9dWb3OmohojEfWuhoUg==","shasum":"10a00c5ad7806d7b84619a5599ea443bdf1bb0d8","tarball":"http://registry-npm.ywwl.com/serialize-error/download/serialize-error-7.0.0.tgz","fileCount":5,"unpackedSize":6759,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJet6d9CRA9TVsSAnZWagAAr8kQAJjArf/VS2jq+fuyj9vE\njg6liiwD+BBa6GVxjPzgtmXMQZXCs1yWQjrVAFU12+Jbi/nD5YLVgoU7Gif0\nuv2eCrZI4/42SyOwdRsYGNx15V5eALEnyiMhS/Y9XY8H0fqIGJ0iBGXIPbi8\nSRO0oA8iZLUJrPCFX+ovLphl/yf7n7eQz6Z+MeebJsh52ldHw/+VHKhgczGs\ns5p1B3GHT1a7w8fPsB1cak2lSPnAomJFFwX1dwGz5pWw5JxkcEEtKxD51T7J\n8RwmcbiP+354W1oIBevFO/Y7p7BxrJ9OiCsrbopml13Jjzzg5AwpVQ5uiWJY\nqCgB5+CVo0T8WcxoR4DbkvxbeS5dMysL1Dn81wLsjSvjnrMwGdRPmzYjdQ14\n9+qNJz3rA1GxG000jYHQ4ZKjNHO4Z8iN/MF19OrclYvN6Jh5MR+zYRWXv5lv\n7SDtK/EAtNqMSwfEhqb3OkIaYfhNHIhvYC4OcqVioqEWwVN+xW7XwZMv9Cdi\nqCr4K3YOIPwdS1BmFl1rPzsL3fyVr0UpaZaVbmC2cIThAiGcMwp8zjDAIXS/\n6tUwwAzv+YgEppA8hXjTNDlRK4UN/cRwMml21bJ0tkpfcRnK2Qf2hGKi9mdr\nZAYDwtRWwS8IxpvC3ab3UdKQl9eS5TgSSkfxorbN4NdxOlEQLFoyAR7tg0sI\n0kwZ\r\n=Vu8g\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDgNpsjQbzT52nDM6touc9AWpsKNQX+gjjx0I7vviVRbQIgDru8F4P6HcY7nS3TfqyGqGQcZACKZ9VbLnH9BD1h/2Y="}],"size":2716,"noattachment":false,"key":"/serialize-error/-/serialize-error-7.0.0.tgz"},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/serialize-error_7.0.0_1589094268694_0.4737311091370897"},"_hasShrinkwrap":false,"readme":"# serialize-error\n\n> Serialize/deserialize an error into a plain object\n\nUseful if you for example need to `JSON.stringify()` or `process.send()` the error.\n\n## Install\n\n```sh\nnpm install serialize-error\n```\n\n## Usage\n\n```js\nimport {serializeError, deserializeError} from 'serialize-error';\n\nconst error = new Error('🦄');\n\nconsole.log(error);\n//=> [Error: 🦄]\n\nconst serialized = serializeError(error);\n\nconsole.log(serialized);\n//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\\n    at Object.<anonymous> …'}\n\nconst deserialized = deserializeError(serialized);\n\nconsole.log(deserialized);\n//=> [Error: 🦄]\n```\n\n### Error constructors\n\nWhen a serialized error with a known `name` is encountered, it will be deserialized using the corresponding error constructor, while unknown error names will be deserialized as regular errors:\n\n```js\nimport {deserializeError} from 'serialize-error';\n\nconst known = deserializeError({\n\tname: 'TypeError',\n\tmessage: '🦄'\n});\n\nconsole.log(known);\n//=> [TypeError: 🦄] <-- Still a TypeError\n\nconst unknown = deserializeError({\n\tname: 'TooManyCooksError',\n\tmessage: '🦄'\n});\n\nconsole.log(unknown);\n//=> [Error: 🦄] <-- Just a regular Error\n```\n\nThe [list of known errors](./error-constructors.js) can be extended globally. This also works if `serialize-error` is a sub-dependency that's not used directly.\n\n```js\nimport {errorConstructors} from 'serialize-error';\nimport {MyCustomError} from './errors.js'\n\nerrorConstructors.set('MyCustomError', MyCustomError)\n```\n\n**Warning:** Only simple and standard error constructors are supported, like `new MyCustomError(message)`. If your error constructor **requires** a second parameter or does not accept a string as first parameter, adding it to this map **will** break the deserialization.\n\n## API\n\n### serializeError(value, options?)\n\nSerialize an `Error` object into a plain object.\n\n- Non-error values are passed through.\n- Custom properties are preserved.\n- Non-enumerable properties are kept non-enumerable (name, message, stack).\n- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).\n- Buffer properties are replaced with `[object Buffer]`.\n- Circular references are handled.\n- If the input object has a `.toJSON()` method, then it's called instead of serializing the object's properties.\n- It's up to `.toJSON()` implementation to handle circular references and enumerability of the properties.\n\n### value\n\nType: `Error | unknown`\n\n### toJSON implementation examples\n\n```js\nimport {serializeError} from 'serialize-error';\n\nclass ErrorWithDate extends Error {\n\tconstructor() {\n\t\tsuper();\n\t\tthis.date = new Date();\n\t}\n}\n\nconst error = new ErrorWithDate();\n\nserializeError(error);\n// => {date: '1970-01-01T00:00:00.000Z', name, message, stack}\n```\n\n```js\nimport {serializeError} from 'serialize-error';\n\nconst error = new Error('Unicorn');\n\nerror.horn = {\n\ttoJSON() {\n\t\treturn 'x';\n\t}\n};\n\nserializeError(error);\n// => {horn: 'x', name, message, stack}\n```\n\n### deserializeError(value, options?)\n\nDeserialize a plain object or any value into an `Error` object.\n\n- `Error` objects are passed through.\n- Objects that have at least a `message` property are interpreted as errors.\n- All other values are wrapped in a `NonError` error.\n- Custom properties are preserved.\n- Non-enumerable properties are kept non-enumerable (name, message, stack, cause).\n- Enumerable properties are kept enumerable (all properties besides the non-enumerable ones).\n- Circular references are handled.\n- [Native error constructors](./error-constructors.js) are preserved (TypeError, DOMException, etc) and [more can be added.](#error-constructors)\n\n### value\n\nType: `{message: string} | unknown`\n\n### options\n\nType: `object`\n\n#### maxDepth\n\nType: `number`\\\nDefault: `Number.POSITIVE_INFINITY`\n\nThe maximum depth of properties to preserve when serializing/deserializing.\n\n```js\nimport {serializeError} from 'serialize-error';\n\nconst error = new Error('🦄');\nerror.one = {two: {three: {}}};\n\nconsole.log(serializeError(error, {maxDepth: 1}));\n//=> {name: 'Error', message: '🦄', one: {}}\n\nconsole.log(serializeError(error, {maxDepth: 2}));\n//=> {name: 'Error', message: '🦄', one: { two: {}}}\n```\n\n#### useToJSON\n\nType: `boolean`\\\nDefault: `true`\n\nIndicate 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 to not use it.\n\n### isErrorLike(value)\n\nPredicate to determine whether a value looks like an error, even if it's not an instance of `Error`. It must have at least the `name`, `message`, and `stack` properties.\n\n```js\nimport {isErrorLike} from 'serialize-error';\n\nconst error = new Error('🦄');\nerror.one = {two: {three: {}}};\n\nisErrorLike({\n\tname: 'DOMException',\n\tmessage: 'It happened',\n\tstack: 'at foo (index.js:2:9)',\n});\n//=> true\n\nisErrorLike(new Error('🦄'));\n//=> true\n\nisErrorLike(serializeError(new Error('🦄'));\n//=> true\n\nisErrorLike({\n\tname: 'Bluberricious pancakes',\n\tstack: 12,\n\tingredients: 'Blueberry',\n});\n//=> false\n```\n","publish_time":1589094268802,"_cnpm_publish_time":1589094268802,"dist-tags":{"latest":"13.1.0"}}