![Medium](https://img.shields.io/badge/%E2%80%8B-medium-brightgreen.svg?logo=medium)
Convert errors to/from plain objects.
Features
Example
import { serialize, parse } from 'error-serializer'
const error = new TypeError('example')
const errorObject = serialize(error)
const errorString = JSON.serialize(errorObject)
const newErrorObject = JSON.parse(errorString)
const newError = parse(newErrorObject)
Install
npm install error-serializer
This package is an ES module and must be loaded using
an import
or import()
statement,
not require()
.
API
serialize(errorInstance, options?)
errorInstance
any
options
Options?
Return value: object
Convert an Error
instance into a plain object.
Options
Object with the following optional properties.
loose
Type: boolean
Default: false
If this option is true
and errorInstance
is not an Error
instance, it is
returned as is, instead of being converted to a plain object.
console.log(serialize('example'))
console.log(serialize('example', { loose: true }))
parse(errorObject, options?)
errorObject
any
options
Options?
Return value: Error
Convert an error plain object into an Error
instance.
Options
Object with the following optional properties.
types
Type: object
Custom error types to keep when parsing.
- Each key is an
errorObject.name
. - Each value is the error type to use. The constructor will be called with a
single
message
argument. It it throws, Error
will be used as the error
type instead.
const errorObject = serialize(new CustomError('example'))
const error = parse(errorObject, { types: { CustomError } })
const otherError = parse(errorObject, { types: { CustomError: TypeError } })
loose
Type: boolean
Default: false
If this option is true
and errorObject
is not an error plain object, it is
returned as is, instead of being converted to an Error
instance.
console.log(parse('example'))
console.log(parse('example', { loose: true }))
Usage
JSON safety
Error plain objects are always
safe to serialize with JSON.
const error = new Error('example')
error.cycle = error
console.log(serialize(error).cycle)
error.toJSON()
serialize()
can be used as
error.toJSON()
.
class CustomError extends Error {
toJSON() {
return serialize(this)
}
}
const error = new CustomError('example')
console.log(error.toJSON())
console.log(JSON.stringify(error))
Deep serialization/parsing
Objects and arrays containing errors can be deeply serialized/parsed using the
loose
option combined with
JSON.stringify()
's replacer
and
JSON.parse()
's reviver.
const deepObject = [{}, { error: new Error('example') }]
const jsonString = JSON.stringify(deepObject, (key, value) =>
serialize(value, { loose: true }),
)
const newDeepObject = JSON.parse(jsonString, (key, value) =>
parse(value, { loose: true }),
)
console.log(newDeepObject[1].error)
Custom serialization/parsing
Errors are converted to/from plain objects, not strings. This allows any
serialization/parsing logic to be performed.
import { dump, load } from 'js-yaml'
const error = new Error('example')
const errorObject = serialize(error)
const errorYamlString = dump(errorObject)
const newErrorObject = load(errorYamlString)
const newError = parse(newErrorObject)
Additional error properties
const error = new TypeError('example')
error.prop = true
const errorObject = serialize(error)
console.log(errorObject.prop)
const newError = parse(errorObject)
console.log(newError.prop)
error.cause
and AggregateError
const innerErrors = [new Error('one'), new Error('two')]
const cause = new Error('three')
const error = new AggregateError(innerErrors, 'four', { cause })
const errorObject = serialize(error)
const newError = parse(errorObject)
Error normalization
Invalid error instances or objects are
normalized.
console.log(serialize('example'))
console.log(parse({ name: false, message: 'example' }))
Related projects
Support
For any question, don't hesitate to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a
Code of conduct in order to promote a positive and
inclusive environment.
Contributing
This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.
If the documentation is unclear or has a typo, please click on the page's Edit
button (pencil icon) and suggest a correction.
If you would like to help us fix a bug or add a new feature, please check our
guidelines. Pull requests are welcome!