JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 7987
  • Score
    100M100P100Q123731F
  • License Apache-2.0

Convert errors to/from plain objects

Package Exports

  • error-serializer

Readme

Codecov Node TypeScript Twitter Medium

Convert errors to/from plain objects.

Features

Example

import { serialize, parse } from 'error-serializer'

const error = new TypeError('example')
const errorObject = serialize(error)
// Plain object: { name: 'TypeError', message: 'example', stack: '...' }

const errorString = JSON.serialize(errorObject)
const newErrorObject = JSON.parse(errorString)

const newError = parse(newErrorObject)
// Error instance: 'TypeError: example ...'

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)

errorInstance any
Return value: object

Convert an Error instance into a plain object.

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'))
// `CustomError` type is kept
const error = parse(errorObject, { types: { CustomError } })
// Map `CustomError` to another type
const otherError = parse(errorObject, { types: { CustomError: TypeError } })

Usage

JSON safety

Error plain objects are always safe to serialize with JSON.

const error = new Error('example')
error.cycle = error

// Cycles make `JSON.serialize()` throw, so they are removed
console.log(serialize(error).cycle) // {}

Custom serialization

serialize() returns a plain object, not a string. This allows any serialization logic to be performed.

import { dump } from 'js-yaml'

const error = new Error('example')
const errorObject = serialize(error)
const errorYamlString = dump(errorObject)
// name: Error
// message: example
// stack: Error: example ...

Additional error properties

const error = new TypeError('example')
error.prop = true

const errorObject = serialize(error)
console.log(errorObject.prop) // true
const newError = parse(errorObject)
console.log(newError.prop) // true

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)
// {
//   name: 'AggregateError',
//   message: 'four',
//   stack: '...',
//   cause: { name: 'Error', message: 'three', stack: '...' },
//   errors: [{ name: 'Error', message: 'one', stack: '...' }, ...],
// }
const newError = parse(errorObject)
// AggregateError: four
//   [cause]: Error: three
//   [errors]: [Error: one, Error: two]

Error normalization

Invalid error instances or objects are normalized.

// Normalizes invalid error: not an `Error` instance
console.log(serialize('example')) // { name: 'Error', message: 'example', ... }

// Normalizes `error.message`: not a string
console.log(parse({ message: false })) // Error: false

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!