serialize-error
Advanced tools
Comparing version 4.1.0 to 5.0.0
import {Primitive, JsonObject} from 'type-fest'; | ||
declare namespace serializeError { | ||
type ErrorObject = { | ||
name?: string; | ||
stack?: string; | ||
message?: string; | ||
code?: string; | ||
} & JsonObject; | ||
} | ||
export type ErrorObject = { | ||
name?: string; | ||
stack?: string; | ||
message?: string; | ||
code?: string; | ||
} & JsonObject; | ||
declare const serializeError: { | ||
/** | ||
Serialize an error into a plain object. | ||
/** | ||
Serialize an `Error` object into a plain object. | ||
@example | ||
``` | ||
import serializeError = require('serialize-error'); | ||
Non-error values are passed through. | ||
Custom properties are preserved. | ||
Circular references are handled. | ||
const error = new Error('🦄'); | ||
@example | ||
``` | ||
import {serializeError} from 'serialize-error'; | ||
console.log(error); | ||
//=> [Error: 🦄] | ||
const error = new Error('🦄'); | ||
console.log(serializeError(error)); | ||
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} | ||
``` | ||
*/ | ||
<ErrorType>(error: ErrorType): ErrorType extends Primitive | ||
? ErrorType | ||
: serializeError.ErrorObject; | ||
console.log(error); | ||
//=> [Error: 🦄] | ||
// TODO: Remove this for the next major release, refactor the whole definition to: | ||
// declare function serializeError<ErrorType>( | ||
// error: ErrorType | ||
// ): ErrorType extends Primitive ? ErrorType : ErrorObject; | ||
// export = serializeError; | ||
default: typeof serializeError; | ||
}; | ||
console.log(serializeError(error)); | ||
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} | ||
``` | ||
*/ | ||
export function serializeError<ErrorType>(error: ErrorType): ErrorType extends Primitive | ||
? ErrorType | ||
: ErrorObject; | ||
export = serializeError; | ||
/** | ||
Deserialize a plain object or any value into an `Error` object. | ||
`Error` objects are passed through. | ||
Non-error values are wrapped in a `NonError` error. | ||
Custom properties are preserved. | ||
Circular references are handled. | ||
@example | ||
``` | ||
import {deserializeError} from 'serialize-error'; | ||
const error = deserializeError({ | ||
message: 'aaa', | ||
stack: 'at <anonymous>:1:13' | ||
}); | ||
console.log(error); | ||
// Error: aaa | ||
// at <anonymous>:1:13 | ||
``` | ||
*/ | ||
export function deserializeError(errorObject: ErrorObject | unknown): Error; |
50
index.js
'use strict'; | ||
const {inspect} = require('util'); | ||
const destroyCircular = (from, seen) => { | ||
const to = Array.isArray(from) ? [] : {}; | ||
class NonError extends Error { | ||
constructor(message) { | ||
super(inspect(message)); | ||
this.name = 'NonError'; | ||
Error.captureStackTrace(this, NonError); | ||
} | ||
} | ||
const commonProperties = [ | ||
'name', | ||
'message', | ||
'stack', | ||
'code' | ||
]; | ||
const destroyCircular = (from, seen, to_) => { | ||
const to = to_ || (Array.isArray(from) ? [] : {}); | ||
seen.push(from); | ||
@@ -26,9 +42,2 @@ | ||
const commonProperties = [ | ||
'name', | ||
'message', | ||
'stack', | ||
'code' | ||
]; | ||
for (const property of commonProperties) { | ||
@@ -44,3 +53,3 @@ if (typeof from[property] === 'string') { | ||
const serializeError = value => { | ||
if (typeof value === 'object') { | ||
if (typeof value === 'object' && value !== null) { | ||
return destroyCircular(value, []); | ||
@@ -58,4 +67,19 @@ } | ||
module.exports = serializeError; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = serializeError; | ||
const deserializeError = value => { | ||
if (value instanceof Error) { | ||
return value; | ||
} | ||
if (typeof value === 'object' && value !== null && !Array.isArray(value)) { | ||
const newError = new Error(); | ||
destroyCircular(value, [], newError); | ||
return newError; | ||
} | ||
return new NonError(value); | ||
}; | ||
module.exports = { | ||
serializeError, | ||
deserializeError | ||
}; |
{ | ||
"name": "serialize-error", | ||
"version": "4.1.0", | ||
"description": "Serialize an error into a plain object", | ||
"version": "5.0.0", | ||
"description": "Serialize/deserialize an error into a plain object", | ||
"license": "MIT", | ||
@@ -29,12 +29,13 @@ "repository": "sindresorhus/serialize-error", | ||
"process", | ||
"send" | ||
"send", | ||
"deserialize" | ||
], | ||
"dependencies": { | ||
"type-fest": "^0.3.0" | ||
"type-fest": "^0.8.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.1", | ||
"ava": "^2.4.0", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
# serialize-error [![Build Status](https://travis-ci.org/sindresorhus/serialize-error.svg?branch=master)](https://travis-ci.org/sindresorhus/serialize-error) | ||
> Serialize an error into a plain object | ||
> Serialize/deserialize an error into a plain object | ||
@@ -18,3 +18,3 @@ Useful if you for example need to `JSON.stringify()` or `process.send()` the error. | ||
```js | ||
const serializeError = require('serialize-error'); | ||
const {serializeError, deserializeError} = require('serialize-error'); | ||
@@ -26,9 +26,32 @@ const error = new Error('🦄'); | ||
console.log(serializeError(error)); | ||
const serialized = serializeError(error) | ||
console.log(serialized); | ||
//=> {name: 'Error', message: '🦄', stack: 'Error: 🦄\n at Object.<anonymous> …'} | ||
const deserialized = deserializeError(serialized); | ||
//=> [Error: 🦄] | ||
``` | ||
## API | ||
## License | ||
### serializeError(value) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
Type: `Error | unknown` | ||
Serialize an `Error` object into a plain object. | ||
Non-error values are passed through. | ||
Custom properties are preserved. | ||
Circular references are handled. | ||
### deserializeError(value) | ||
Type: `{[key: string]: unknown} | unknown` | ||
Deserialize a plain object or any value into an `Error` object. | ||
`Error` objects are passed through. | ||
Non-error values are wrapped in a `NonError` error. | ||
Custom properties are preserved. | ||
Circular references are handled. |
5874
109
56
+ Addedtype-fest@0.8.1(transitive)
- Removedtype-fest@0.3.1(transitive)
Updatedtype-fest@^0.8.0