Prepare an Error for JSON serialization
npm install serialize-http-error


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).
``js
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));
});
`
The second argument to serializeHttpError may be an object with options, eg:
`js`
serializeHttpError(err, {
unsafe: true,
flat: true,
expose: false
});
> false
If set to true, all enumerable properties, even recursive ones, will bereplacer
copied. This allows for customized resolution of these properties, for example
by using the argument in JSON.stringify.
> false
By default, nested Error objects are also serialized. If set to true, they
will be left intact.
> process.env.NODE_ENV === 'development'
If set to true, all errors will be exposed. If set to false, only exposable
errors are exposed.
> 'Error'
The default name to use for values which don't have a name, or errors which
may not be exposed.
> 'Something went wrong'`
The default message to use for values which don't have a message, or errors
which may not be exposed.