Skip to content

Commit

Permalink
Merge pull request #22 from marsanla/master
Browse files Browse the repository at this point in the history
Add error handler, thanks
  • Loading branch information
z0mt3c committed Nov 25, 2014
2 parents 0e8b9dc + a06f9c3 commit 80b2e34
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ results

npm-debug.log
coverage.html
/.project
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -17,7 +17,10 @@ Example:

var server = restify.createServer();
server.use(restify.queryParser());
server.use(restifyValidation.validationPlugin( { errorsAsArray: false }));
server.use(restifyValidation.validationPlugin( {
errorsAsArray: false,
errorHandler: restify.errors.InvalidArgumentError
}));

server.get({url: '/test/:name', validation: {
name: { isRequired: true, isIn: ['foo','bar'], scope: 'path' },
Expand All @@ -32,6 +35,7 @@ Example:
console.log('%s listening at %s', server.name, server.url);
});


## Use
Simply install it through npm

Expand All @@ -41,9 +45,11 @@ Simply install it through npm
## Documentation powered by swagger
On top of the validation schema the [node-restify-swagger](https://github.com/z0mt3c/node-restify-swagger) library should later-on generate the swagger resources to provide a hands-on documentation.


## Demo project
A simple demo project can be cloned from [node-restify-demo](https://github.com/z0mt3c/node-restify-demo).


## Supported validations

isRequired: true | function()
Expand Down
12 changes: 8 additions & 4 deletions lib/error.js
Expand Up @@ -3,8 +3,12 @@
*/

module.exports.handle = function (errors, req, res, options, next) {
return res.send(400, {
status: 'validation failed',
errors: errors
});
if(options.errorHandler) {
return res.send(new options.errorHandler(errors));
} else {
return res.send (400, {
status: 'validation failed',
errors: errors
});
}
};
3 changes: 2 additions & 1 deletion lib/index.js
Expand Up @@ -10,7 +10,8 @@ module.exports.error = require('./error');
module.exports.when = require('./conditions');

var defaultOptions = {
errorsAsArray: true
errorsAsArray: true,
errorHandler: false
};

module.exports.validationPlugin = function (options) {
Expand Down
5 changes: 4 additions & 1 deletion package.json
@@ -1,9 +1,12 @@
{
"name": "node-restify-validation",
"version": "0.1.1",
"version": "0.1.2",
"author": {
"name": "Timo Behrmann"
},
"contributors": {
"name": "Marcos Sanz"
},
"license": "MIT",
"keywords": [
"rest",
Expand Down
4 changes: 2 additions & 2 deletions test/errors_test.js
Expand Up @@ -15,7 +15,7 @@ describe('Errors', function () {
var res = { send: send };
var errors = {};

index.error.handle(errors, null, res, null, next);
index.error.handle(errors, null, res, {}, next);

next.called.should.not.be.ok;
send.calledWith(400, {
Expand All @@ -33,7 +33,7 @@ describe('Errors', function () {
var res = { send: send };
var errors = [];

index.error.handle(errors, null, res, null, next);
index.error.handle(errors, null, res, {}, next);

next.called.should.not.be.ok;
send.calledWith(400, {
Expand Down

0 comments on commit 80b2e34

Please sign in to comment.