Skip to content

Commit db7a463

Browse files
author
simonepri
committed
Update documentation
1 parent 234fd82 commit db7a463

1 file changed

Lines changed: 82 additions & 36 deletions

File tree

README.md

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,101 @@
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 [![Travis CI](https://travis-ci.org/simonepri/restify-errors-thrower.svg?branch=master)](https://travis-ci.org/simonepri/restify-errors-thrower) [![Codecov](https://img.shields.io/codecov/c/github/simonepri/restify-errors-thrower/master.svg)](https://codecov.io/gh/simonepri/restify-errors-thrower) [![npm](https://img.shields.io/npm/dm/restify-errors-thrower.svg)](https://www.npmjs.com/package/restify-errors-thrower) [![npm version](https://img.shields.io/npm/v/restify-errors-thrower.svg)](https://www.npmjs.com/package/restify-errors-thrower)
2+
> 💥 Throw Restify errors easily and consistently!
53
6-
## Getting Started
74

8-
Install the module with: `npm install restify-errors-thrower`
5+
## Install
96

10-
## Usage
11-
12-
13-
### Creating Errors
7+
```
8+
$ npm install --save restify-errors-thrower
9+
```
1410

15-
In your application, create errors by using the constructors:
11+
## Usage
1612

1713
```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+
});
1922

23+
// Creates a foo endpoint
2024
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+
});
2141

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;
2548

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+
}
2854
});
2955
```
3056
31-
### Checking Error types
57+
## API
3258
33-
You can easily do instance checks against the Error objects:
59+
### throw(type, message, errno, [debug])
3460
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.
4962
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
5180
52-
Copyright (c) 2016 Simone Primarosa
81+
Type: `string|number`
5382
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!
5586
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

Comments
 (0)