restify-errors-thrower
Version:
Throw Restify errors easily and consistently!
120 lines (82 loc) • 3.75 kB
Markdown
[](https://travis-ci.org/simonepri/restify-errors-thrower) [](https://codecov.io/gh/simonepri/restify-errors-thrower) [](https://www.npmjs.com/package/restify-errors-thrower) [](https://www.npmjs.com/package/restify-errors-thrower) [](https://david-dm.org/simonepri/restify-errors-thrower) [](https://david-dm.org/simonepri/restify-errors-thrower#info=devDependencies)
> 💥 Throw Restify errors easily and consistently!
```
$ npm install --save restify-errors-thrower
```
```js
const restify = require('restify');
const thrower = require('restify-errors-thrower');
// Creates a Restify server
const server = restify.createServer({
name: 'myapp',
version: '1.0.0'
});
// Creates a foo endpoint
server.get('/foo', function(req, res, next) {
if (!req.query.foo) {
return next(thrower.throw('BadRequestError', 'foo undefined', 'R.FOO.0');
}
if (req.query.foo !== 'unicorn') {
return next(thrower.throw('BadRequestError', 'foo should be an 🦄!', 'R.FOO.1');
}
// Adds debug info
const keys = req.query.keys();
if (keys.length > 1) {
return next(thrower.throw('BadRequestError', 'Only foo allowed', 'R.FOO.2', keys);
}
res.send(200, 'ok!');
return next();
});
// Logs errors and debug info
server.on('after', function(req, res, route, err) {
if (err && err.context) {
const method = req.method.toUpperCase();
const uri = req._url.path;
const endpoint = method + '\t' + uri;
console.log('[API]', endpoint, err.toString() + ' (' + err.context.errno + ')');
if(err.context.debug) {
debug.forEach(d => console.log('[DEBUG]', endpoint, d);
}
}
});
```
Throw a specific Restify error.
Type: `string`
The type of error to throw.
The list of types available can be found [here](https://github.com/restify/errors#restify-errors)
Type: `string`
An human-friendly error message sent to the client.<br>
**Never sent error messages that comes from other modules!!!** (E.g: your database)<br>
This may expose you to undesired hackers attack!<br>
Use the debug parameter instead for sensitive errors!
Type: `string|number`
An unique error id code to send to clients.<br>
This will help your client to programmatically handle the error your API will throw.<br>
Choose a style and be consistent with it!
Type: `string|number`
An indefinite number of contex information to collect.<br>
This is particular useful to send contex details to your logger!<br>
This will never sent to the client so you can store server critical messages. (E.g; errors coming from third pary APIs or errors coming from your DB)
Checks if a specific Restify error was thrown.
Type: `object`
The object to check
Type: `string`
Default: `undefined`
The type of error that the object should be instance of.
* **Simone Primarosa** - [simonepri](https://github.com/simonepri)
See also the list of [contributors](https://github.com/simonepri/restify-errors-thrower/contributors) who participated in this project.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.