|
1 | | -# restify-errors-thrower |
2 | | -This is an helper module in order to throw restify-errors errors |
3 | | -Detailed informations about error codes available can be found here: |
4 | | -https://github.com/restify/errors |
| 1 | +# 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) |
| 2 | +> 💥 Throw Restify errors easily and consistently! |
5 | 3 |
|
6 | | -## Getting Started |
7 | 4 |
|
8 | | -Install the module with: `npm install restify-errors-thrower` |
| 5 | +## Install |
9 | 6 |
|
10 | | -## Usage |
11 | | - |
12 | | - |
13 | | -### Creating Errors |
| 7 | +``` |
| 8 | +$ npm install --save restify-errors-thrower |
| 9 | +``` |
14 | 10 |
|
15 | | -In your application, create errors by using the constructors: |
| 11 | +## Usage |
16 | 12 |
|
17 | 13 | ```js |
18 | | -var thrower = require('restify-errors-thrower'); |
| 14 | +const restify = require('restify'); |
| 15 | +const thrower = require('restify-errors-thrower'); |
| 16 | + |
| 17 | +// Creates a Restify server |
| 18 | +const server = restify.createServer({ |
| 19 | + name: 'myapp', |
| 20 | + version: '1.0.0' |
| 21 | +}); |
19 | 22 |
|
| 23 | +// Creates a foo endpoint |
20 | 24 | server.get('/foo', function(req, res, next) { |
| 25 | + if (!req.query.foo) { |
| 26 | + return next(thrower.throw('BadRequestError', 'foo undefined', 'R.FOO.0'); |
| 27 | + } |
| 28 | + if (req.query.foo === '42') { |
| 29 | + return next(thrower.throw('BadRequestError', 'foo cannot be the answer to the meaning of life', 'R.FOO.1'); |
| 30 | + } |
| 31 | + |
| 32 | + // Adds debug info |
| 33 | + const keys = req.query.keys(); |
| 34 | + if (keys.length > 1) { |
| 35 | + return next(thrower.throw('BadRequestError', 'Only foo allowed', 'R.FOO.2', keys); |
| 36 | + } |
| 37 | + |
| 38 | + res.send(200, 'ok!'); |
| 39 | + return next(); |
| 40 | +}); |
21 | 41 |
|
22 | | - if (!req.query.foo) { |
23 | | - return next(thrower.throw('BadRequestError', 'foo undefined', 'some contex'); |
24 | | - } |
| 42 | +// Logs errors and debug info |
| 43 | +server.on('after', function(req, res, route, err) { |
| 44 | + if (err && err.context) { |
| 45 | + const method = req.method.toUpperCase(); |
| 46 | + const uri = req._url.path; |
| 47 | + const endpoint = method + '\t' + uri; |
25 | 48 |
|
26 | | - res.send(200, 'ok!'); |
27 | | - return next(); |
| 49 | + console.log('[API]', endpoint, err.toString() + ' (' + err.context.errno + ')'); |
| 50 | + if(err.context.debug) { |
| 51 | + debug.forEach(d => console.log('[DEBUG]', endpoint, d); |
| 52 | + } |
| 53 | + } |
28 | 54 | }); |
29 | 55 | ``` |
30 | 56 |
|
31 | | -### Checking Error types |
| 57 | +## API |
32 | 58 |
|
33 | | -You can easily do instance checks against the Error objects: |
| 59 | +### throw(type, message, errno, [debug]) |
34 | 60 |
|
35 | | -```js |
36 | | -function redirectIfErr(req, res, next) { |
37 | | - var err = req.data.error; |
38 | | - if (err) { |
39 | | - if (thrower.throwed(err,'InternalServerError')) { |
40 | | - next(err); |
41 | | - } else if (thrower.throwed(err,'NotFoundError')) { |
42 | | - res.redirect('/NotFound', next); |
43 | | - } else if (thrower.throwed(err)) { |
44 | | - res.redirect(err); |
45 | | - } |
46 | | - } |
47 | | -} |
48 | | -``` |
| 61 | +Throw a specific Restify error. |
49 | 62 |
|
50 | | -## License |
| 63 | +#### type |
| 64 | +
|
| 65 | +Type: `string` |
| 66 | +
|
| 67 | +The type of error to throw. |
| 68 | +The list of types available can be found [here](https://github.com/restify/errors#restify-errors) |
| 69 | +
|
| 70 | +#### message |
| 71 | +
|
| 72 | +Type: `string` |
| 73 | +
|
| 74 | +An human-friendly error message sent to the client.<br> |
| 75 | +**Never sent error messages that comes from other modules!!!** (E.g: your database)<br> |
| 76 | +This may expose you to undesired hackers attack!<br> |
| 77 | +Use the debug parameter instead for sensitive errors! |
| 78 | +
|
| 79 | +#### errno |
51 | 80 |
|
52 | | -Copyright (c) 2016 Simone Primarosa |
| 81 | +Type: `string|number` |
53 | 82 |
|
54 | | -Licensed under the MIT license. |
| 83 | +An unique error id code to send to clients.<br> |
| 84 | +This will help your client to programmatically handle the error your API will throw.<br> |
| 85 | +Choose a style and be consistent with it! |
55 | 86 |
|
| 87 | +#### debug |
| 88 | +
|
| 89 | +Type: `string|number` |
| 90 | +
|
| 91 | +An indefinite number of contex information to collect.<br> |
| 92 | +This is particular useful to send contex details to your logger!<br> |
| 93 | +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) |
| 94 | +
|
| 95 | +## Authors |
| 96 | +* **Simone Primarosa** - [simonepri](https://github.com/simonepri) |
| 97 | +
|
| 98 | +See also the list of [contributors](https://github.com/simonepri/restify-errors-thrower/contributors) who participated in this project. |
| 99 | +
|
| 100 | +## License |
| 101 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
0 commit comments