{"_id":"@hublo/serialize-error","_rev":"24-27d4b6bac498aefdc192b4170d27b2ee","name":"@hublo/serialize-error","dist-tags":{"latest":"1.1.0"},"versions":{"1.0.0":{"name":"@hublo/serialize-error","version":"1.0.0","keywords":["error","serialize","stringify","object","convert","process","send","cause","deserialize"],"license":"MIT","_id":"@hublo/serialize-error@1.0.0","maintainers":[{"name":"jacobdelcroix","email":"delcroix.jacob@gmail.com"},{"name":"vivienhublo","email":"vivien.marciniak@hublo.com"},{"name":"leoclement","email":"leo.clement@hublo.com"},{"name":"thomasrovayazhublo","email":"thomas.rovayaz-ext@hublo.com"},{"name":"vincent-hublo","email":"vincent.durand@hublo.com"},{"name":"tirke","email":"tirke@tirke.dev"},{"name":"chrismedgo","email":"chris@medgo.fr"}],"dist":{"shasum":"969b80f0edc7c601e22a095b3c8f3096b37791be","tarball":"https://registry.npmjs.org/@hublo/serialize-error/-/serialize-error-1.0.0.tgz","fileCount":6,"integrity":"sha512-Dlc64ZDhrUWoTKo3g+1rUvjWRtntxk8mGs5dU4IvSUHGlZ722izuE7tFibV/VyhBs1smSFWS76NSeYbM8MgpYg==","signatures":[{"sig":"MEUCIBD+dmAHa/IHVSWQT36A1Vq4lHsk1BRN+IQDOWc9udl0AiEAtpU00A047X0uHhuCI43yLMy3hBCPsXArPm/b6PjJDPQ=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15254},"main":"index.js","_from":"file:hublo-serialize-error-1.0.0.tgz","types":"./index.d.ts","scripts":{"test":"ava && tsd"},"_npmUser":{"name":"tirke","email":"tirke@tirke.dev"},"_resolved":"/private/var/folders/dn/zfmw02m54f125nx0sbdpmc6w0000gn/T/aec6890e060377fe1c5c030bc11d3bc3/hublo-serialize-error-1.0.0.tgz","_integrity":"sha512-Dlc64ZDhrUWoTKo3g+1rUvjWRtntxk8mGs5dU4IvSUHGlZ722izuE7tFibV/VyhBs1smSFWS76NSeYbM8MgpYg==","_npmVersion":"9.5.1","description":"Serialize/deserialize an error into a plain object","directories":{},"_nodeVersion":"18.16.0","dependencies":{"type-fest":"^3.12.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"packageManager":"pnpm@8.6.3","devDependencies":{"ava":"^5.3.1","tsd":"^0.28.1","prettier":"^2.8.8"},"_npmOperationalInternal":{"tmp":"tmp/serialize-error_1.0.0_1687270965251_0.45445859010549006","host":"s3://npm-registry-packages"}},"1.1.0":{"name":"@hublo/serialize-error","version":"1.1.0","keywords":["error","serialize","stringify","object","convert","process","send","cause","deserialize"],"license":"MIT","_id":"@hublo/serialize-error@1.1.0","maintainers":[{"name":"laure.hublo","email":"laure.milian@hublo.com"},{"name":"valentincommenge-hublo","email":"valentin.commenge@hublo.com"},{"name":"jacobdelcroix","email":"delcroix.jacob@gmail.com"},{"name":"vivienhublo","email":"vivien.marciniak@hublo.com"},{"name":"leoclement","email":"leo.clement@hublo.com"},{"name":"tirke","email":"tirke@tirke.dev"}],"dist":{"shasum":"61aaedf53e978cf55073931217cad417f5137802","tarball":"https://registry.npmjs.org/@hublo/serialize-error/-/serialize-error-1.1.0.tgz","fileCount":6,"integrity":"sha512-BltOVXGSbUxnMUl6Bn6hHg6HpkvZKYtbQLWZN4lzkMa+DcLvVgyFlnZy0Gi+2XzDjUTKDzkzQ8F/ohHzCl2ztQ==","signatures":[{"sig":"MEUCIQCN/eHj2WES6BP3rhPzMImaf+iq0edZFhUr1Plbmw6znAIgFj++aLBwnraMoQo7D5znlHdv3ymcbruCNw9bLLsu/w4=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15437},"main":"index.js","_from":"file:hublo-serialize-error-1.1.0.tgz","types":"./index.d.ts","scripts":{"test":"ava && tsd","//test":"xo && ava && tsd"},"_npmUser":{"name":"tirke","email":"tirke@tirke.dev"},"_resolved":"/private/var/folders/dn/zfmw02m54f125nx0sbdpmc6w0000gn/T/b71a0fa9d1454a2cdbe3285e0b13021e/hublo-serialize-error-1.1.0.tgz","_integrity":"sha512-BltOVXGSbUxnMUl6Bn6hHg6HpkvZKYtbQLWZN4lzkMa+DcLvVgyFlnZy0Gi+2XzDjUTKDzkzQ8F/ohHzCl2ztQ==","_npmVersion":"10.2.4","description":"Serialize/deserialize an error into a plain object","directories":{},"sideEffects":false,"_nodeVersion":"20.11.0","dependencies":{"type-fest":"^3.12.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"packageManager":"pnpm@8.6.3","devDependencies":{"ava":"^5.3.1","tsd":"^0.28.1","prettier":"^2.8.8"},"_npmOperationalInternal":{"tmp":"tmp/serialize-error_1.1.0_1713198729808_0.8821982326294264","host":"s3://npm-registry-packages"}}},"time":{"created":"2023-06-20T14:22:45.100Z","modified":"2026-07-31T08:14:47.659Z","1.0.0":"2023-06-20T14:22:45.444Z","1.1.0":"2024-04-15T16:32:09.963Z"},"license":"MIT","keywords":["error","serialize","stringify","object","convert","process","send","cause","deserialize"],"description":"Serialize/deserialize an error into a plain object","maintainers":[{"email":"tschersach@gmail.com","name":"tirke"},{"email":"valentin.commenge@hublo.com","name":"valentincommenge-hublo"},{"email":"logan.gouget@hublo.com","name":"logangougethublo"},{"email":"ahmad@hublo.com","name":"ahmadkanj"},{"email":"pierre.rolland@hublo.com","name":"prollandhublo"},{"email":"thomas.brouillet@hublo.com","name":"thomas_bro_hublo"},{"email":"adil.ouaali@hublo.com","name":"adil-hublo"},{"email":"eric.tromas-ext@hublo.com","name":"erictromashublo"},{"email":"helabenkhalfallah@hotmail.fr","name":"helabenkhalfallah"}],"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","readmeFilename":"readme.md"}