Gathering detailed insights and metrics for serialize-error-cjs
Gathering detailed insights and metrics for serialize-error-cjs
Gathering detailed insights and metrics for serialize-error-cjs
Gathering detailed insights and metrics for serialize-error-cjs
npm install serialize-error-cjs
Typescript
Module System
Node Version
NPM Version
99.7
Supply Chain
98.4
Quality
75.4
Maintenance
100
Vulnerability
100
License
TypeScript (58.56%)
JavaScript (41.44%)
Total Downloads
3,457,305
Last Day
4,873
Last Week
63,809
Last Month
335,553
Last Year
2,891,296
2 Stars
10 Commits
1 Watching
2 Branches
1 Contributors
Latest Version
0.1.3
Package Id
serialize-error-cjs@0.1.3
Unpacked Size
13.61 kB
Size
4.34 kB
File Count
8
NPM Version
6.14.17
Node Version
14.19.2
Cumulative downloads
Total Downloads
Last day
-69.5%
4,873
Compared to previous day
Last week
-23.8%
63,809
Compared to previous week
Last month
0.9%
335,553
Compared to previous month
Last year
411.4%
2,891,296
Compared to previous year
Serialize/deserialize an error into a plain object in commonjs
Loosely based on serialize-error, but with support for commonjs projects.
Useful if you for example need to JSON.stringify()
or process.send()
the error.
1npm install serialize-error-cjs
1import {serializeError, deserializeError} from 'serialize-error-cjs'; 2 3const error = new Error('🦄'); 4 5console.log(error); 6//=> [Error: 🦄] 7 8const serialized = serializeError(error); 9 10console.log(serialized); 11//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} 12 13const deserialized = deserializeError(serialized); 14 15console.log(deserialized); 16//=> [Error: 🦄]
When 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:
1import {deserializeError} from 'serialize-error'; 2 3const known = deserializeError({ 4 name: 'TypeError', 5 message: '🦄' 6}); 7 8console.log(known); 9//=> [TypeError: 🦄] <-- Still a TypeError 10 11const unknown = deserializeError({ 12 name: 'TooManyCooksError', 13 message: '🦄' 14}); 15 16console.log(unknown); 17//=> [Error: 🦄] <-- Just a regular Error
The list of known errors can be extended globally. This also works if serialize-error-cjs
is a sub-dependency that's not used directly.
1import {errorConstructors} from 'serialize-error-cjs'; 2import {MyCustomError} from './errors.js' 3 4errorConstructors.set('MyCustomError', MyCustomError)
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.
Serialize an Error
object into a plain object.
Type: Error
Deserialize a plain object or any value into an Error
object.
Type: {message: string} | unknown
No vulnerabilities found.
No security vulnerabilities found.