https://github.com/simonepri/restify-errors-thrower
💥 Throw Restify errors easily!
https://github.com/simonepri/restify-errors-thrower
errors rest-errors restify restify-errors restify-errors-thrower throw-errors
Last synced: 9 months ago
JSON representation
💥 Throw Restify errors easily!
- Host: GitHub
- URL: https://github.com/simonepri/restify-errors-thrower
- Owner: simonepri
- License: mit
- Created: 2016-03-09T14:32:49.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-08T13:52:45.000Z (about 8 years ago)
- Last Synced: 2024-10-19T19:53:42.578Z (over 1 year ago)
- Topics: errors, rest-errors, restify, restify-errors, restify-errors-thrower, throw-errors
- Language: JavaScript
- Homepage:
- Size: 20.5 KB
- Stars: 2
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Restify Errors Thrower
[](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!
## Install
```
$ npm install --save restify-errors-thrower
```
## Usage
```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);
}
}
});
```
## API
### throw(type, message, errno, [debug])
Throw a specific Restify error.
#### type
Type: `string`
The type of error to throw.
The list of types available can be found [here](https://github.com/restify/errors#restify-errors)
#### message
Type: `string`
An human-friendly error message sent to the client.
**Never sent error messages that comes from other modules!!!** (E.g: your database)
This may expose you to undesired hackers attack!
Use the debug parameter instead for sensitive errors!
#### errno
Type: `string|number`
An unique error id code to send to clients.
This will help your client to programmatically handle the error your API will throw.
Choose a style and be consistent with it!
#### debug
Type: `string|number`
An indefinite number of contex information to collect.
This is particular useful to send contex details to your logger!
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)
### thrown(err, [type])
Checks if a specific Restify error was thrown.
#### err
Type: `object`
The object to check
#### type
Type: `string`
Default: `undefined`
The type of error that the object should be instance of.
## Authors
* **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.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.