Package Exports
- @m59/error-cereal
- @m59/error-cereal/src/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@m59/error-cereal) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@m59/error-cereal
Serialize a JavaScript error to a regular, json stringifiable object, and deserialize an object into an error.
api
const { serialize_error, deserialize_error } = require('@m59/error-cereal')
serialize_error(error)
Takes an error instance and returns a regular object that you can stringify with JSON.stringify
.
Circular references are replaced with '[Circular]'
.
serialize_error(Object.assign(new Error('an error'), { foo: 'bar' }))
// => { name: 'Error', message: 'an error', stack: 'etc', foo: 'bar' }
deserialize_error(object, { custom_error_constructors = {} })
Takes an object and returns an instance of the error referenced by { name }
. This operation is isomorphic with serialize_error
for error instances as they were originally constructed or with added properties when those properties have values that can be converted back to their original.
Custom error constructors can be passed in and will be used to construct the error for objects with a matching { name }.
deserialize_error(
{ name: 'My_Custom_Error', message: 'error message' },
{
custom_error_constructors: {
My_Custom_Error
}
}
)
// => new My_Custom_Error('error message')