{"_id":"serialize-error@13.1.0","bugs":{"url":"https://github.com/sindresorhus/serialize-error/issues"},"dist":{"shasum":"43104770da92c6b2dc370851dc44af54f2562992","tarball":"http://registry-npm.ywwl.com/serialize-error/download/serialize-error-13.1.0.tgz","fileCount":7,"integrity":"sha512-FT1EL2Pz7lAWA48I6Qb1qHEvGT9ZGz2pb1sYZ9Dhu2t+RmHN6O12WMu7HzFcqFRQaUWEl3Q+++EYBTrZpCoykQ==","signatures":[{"sig":"MEQCIFmrY78xrDmri1nl3uI0IKDaW0gKqPgS4/jdQJJj+CXoAiA6A4EONbYs5STzGQcLP7UDQd9g+PIGDAMU1NXeBRd7Ew==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCoqGKsJgspQxntl54u9n89EXc3uUu7zfspS+hlrsekyAIhANhfL9fwKSFTT/FkMBwygiNVWF1HU+ooVHWBKSVRsygT"}],"unpackedSize":27604,"size":8454,"noattachment":false,"key":"/serialize-error/-/serialize-error-13.1.0.tgz"},"name":"serialize-error","type":"module","types":"./index.d.ts","author":{"url":"https://sindresorhus.com","name":"Sindre Sorhus","email":"sindresorhus@gmail.com"},"engines":{"node":">=20"},"exports":{"types":"./index.d.ts","default":"./index.js"},"funding":"https://github.com/sponsors/sindresorhus","gitHead":"84eb6cfd6a5538c20dbb522d0d2947d37a0799fd","license":"MIT","scripts":{"test":"xo && node --test test.js && tsd"},"version":"13.1.0","_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"homepage":"https://github.com/sindresorhus/serialize-error#readme","keywords":["error","serialize","stringify","object","convert","process","send","cause","deserialize"],"repository":{"url":"git+https://github.com/sindresorhus/serialize-error.git","type":"git"},"_npmVersion":"12.2.0","description":"Serialize/deserialize an error into a plain object","directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"sideEffects":false,"_nodeVersion":"26.12.1","dependencies":{"non-error":"^0.1.0","type-fest":"^5.4.1"},"_hasShrinkwrap":false,"devDependencies":{"xo":"^1.2.3","tsd":"^0.33.0","expect-type":"^1.3.0"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/serialize-error_13.1.0_1793652231679_0.21873094524316183"},"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, as long as the project ends up with a single physical copy of the package. Registrations live in that copy's module state, so a duplicate, which is easy to end up with through version skew or a nested install, keeps its own separate list and does not see them.\n\n```js\nimport {addKnownErrorConstructor} from 'serialize-error';\nimport {MyCustomError} from './errors.js'\n\naddKnownErrorConstructor(MyCustomError);\n```\n\nFor error constructors that require arguments, you can provide a factory function:\n\n```js\nimport {addKnownErrorConstructor} from 'serialize-error';\n\nclass CustomError extends Error {\n\tname = 'CustomError';\n\n\tconstructor(message, options) {\n\t\tsuper(message);\n\t\tthis.code = options.code;\n\t}\n}\n\naddKnownErrorConstructor(CustomError, () => new CustomError('', {code: 'ERR_UNICORN'}));\n```\n\nThe constructor, or the factory if given, must return an extensible `Error` instance, since the payload's properties are added to it.\n\n## API\n\n### serializeError(value, options?)\n\nSerialize an `Error` object into a plain object.\n\n- Custom properties are preserved.\n- All properties are made enumerable (`name`, `message`, `stack`, `cause`, `errors`), so the result survives `JSON.stringify()`.\n- Primitive values (including `null`, `undefined`, strings, numbers, etc.) and functions are wrapped in a `NonError` error and serialized.\n- Buffer properties are replaced with `[object Buffer]`.\n- Stream properties are replaced with `[object Stream]`.\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- A `.toJSON()` that returns the object it received is ignored, since that would hand back the original rather than a copy.\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(0);\n\t}\n}\n\nconst error = new ErrorWithDate();\n\nconsole.log(serializeError(error));\n//=> {name, message, stack, date: '1970-01-01T00:00:00.000Z'}\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// => {name, message, stack, horn: 'x'}\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 with a string `message` property are interpreted as errors. Arrays never are, even with a `message` property.\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`, `errors`).\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: '🦄', stack: 'Error: 🦄\\n    at Object.<anonymous> …', one: {}}\n\nconsole.log(serializeError(error, {maxDepth: 2}));\n//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\\n    at Object.<anonymous> …', 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 be an object with string `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\n## Limitations\n\n- Deep nesting can overflow the call stack. Set `maxDepth` to limit recursion.\n- `Map`, `Set`, `RegExp`, `ArrayBuffer`, and `DataView` serialize as empty objects.\n- `toJSON()` results are returned as-is and may be primitives. They must handle their own circular references and BigInts. `useToJSON` only applies to serialization.\n- Getters and registered constructors/factories can throw. Registered constructors/factories must consistently return extensible errors.\n- Nested objects with both `pipe()` and `on()` methods are treated as streams.\n- `DOMException` usually deserializes as a plain `Error` because its `name` differs from its constructor name.\n- Serialization may reorder properties, including those of nested plain objects.\n- Deserialized errors have non-enumerable error properties. Call `serializeError()` before passing them to `JSON.stringify()`.\n","publish_time":1793652231772,"_cnpm_publish_time":1793652231772,"dist-tags":{"latest":"13.1.0"}}