JSPM

@m59/error-cereal

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 20
  • Score
    100M100P100Q33361F
  • License CC0-1.0

Serialize a JavaScript error to a regular, json stringifiable object, and deserialize an object into an error.

Package Exports

  • @m59/error-cereal

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 { serializeError, deserializeError } = require('@m59/error-cereal')

serializeError(error)

Takes an error instance and returns a regular object that you can stringify with JSON.stringify. Circular references are replaced with '[Circular]'.

serializeError(Object.assign(new Error('an error'), { foo: 'bar' }))
// => { name: 'Error', message: 'an error', stack: 'etc', foo: 'bar' }

deserializeError(object, { customErrorConstructors = {} })

Takes an object and returns an instance of the error referenced by { name }. This operation is isomorphic with serializeError 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 }.

deserializeError(
    { name: 'MyCustomError', message: 'error message' },
    {
        customErrorConstructors: {
            MyCustomError
        }
    }
)
// => new MyCustomError('error message')