1.1.0 • Published 2 years ago

serialize-http-error v1.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
2 years ago

Serialize HTTP Error

NPM Version Build Status Code Coverage

Serializes any input (preferrably an Error) to a plain old JavaScript Object which has the following guarantees:

  1. The error will be exposed if:

    • The NODE_ENV is "development", or;
    • The expose option is true, or;
    • the expose property is true, or;
    • the status property contains a number less than 500.
  2. Has at least a name property and a message property, both always Strings. They default to "Error" and "Something went wrong"

  3. Other enumerable properties are copied under the following conditions:

    • The error is exposed, and;
    • the property is safe for JSON.stringify (unless unsafe).

Usage

const serializeHttpError = require('serialize-http-error');
const app = require('express')();

app.use((err, req, res, next) => {
  res.status(err.status || 500);
  res.json(serializeHttpError(err));
});

Options

The second argument to serializeHttpError may be an object with options, eg:

serializeHttpError(err, {
  unsafe: true,
  flat: true,
  expose: false
});

unsafe

false

If set to true, all enumerable properties, even recursive ones, will be copied. This allows for customized resolution of these properties, for example by using the replacer argument in JSON.stringify.

flat

false

By default, nested Error objects are also serialized. If set to true, they will be left intact.

expose

process.env.NODE_ENV === 'development'

If set to true, all errors will be exposed. If set to false, only exposable errors are exposed.

defaultName

'Error'

The default name to use for values which don't have a name, or errors which may not be exposed.

defaultMessage

'Something went wrong'

The default message to use for values which don't have a message, or errors which may not be exposed.