0.3.2 • Published 6 years ago

restify-errors-options v0.3.2

Weekly downloads
49
License
MIT
Repository
github
Last release
6 years ago

restify-errors-options

Travis CI Codecov npm npm version npm dependencies npm dev dependencies

🔧 Add custom options to Restify's errors!

Install

$ npm install --save restify-errors-options

Usage

const errorsOptions = require('restify-errors-options');
// Is extremely important to require restify-errors-options before restify.
const errors = require('restify-errors');

// Default behaviour
const err1 = new errors.NotFoundError({errno: 'NFE'});
console.log(err1.toJSON());
//=> {code: 'NotFound', message: ''}

// Add errno as option to add to the body
errorsOptions.add('errno');
const err2 = new errors.NotFoundError({errno: 'NFE'});
console.log(err2.toJSON());
//=> {code: 'NotFound', message: '', errno: 'NFE'}
console.log(err1.toJSON());
//=> {code: 'NotFound', message: ''}

// Restore the default behaviour
errorsOptions.delete('errno');
const err3 = new errors.NotFoundError({errno: 'NFE'});
console.log(err3.toJSON());
//=> {code: 'NotFound', message: ''}
console.log(err2.toJSON());
//=> {code: 'NotFound', message: '', errno: 'NFE'}
console.log(err1.toJSON());
//=> {code: 'NotFound', message: ''}

Plugins

Community packages that implement adds options through this package. If you want yours listed here open a PR.

API

add(optName, optDefault)

Adds custom options to errors' body.

optName

Type: string

Name of the option key to add.

optDefault

Type: (number|boolean|string|object)

Default value for the option. You can also provide a function, see the next section.

optDefault(errorCode, errorHttpCode, errorMessage)

Type: function

Returns the default value for the option using parameters of the error.

delete(optName)

Removes previously added custom options..

optName

Type: string

Name of the option key to remove.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.