Skip to content

Commit

Permalink
added error handler input.
Browse files Browse the repository at this point in the history
  • Loading branch information
cainus committed Oct 11, 2013
1 parent 542ca78 commit 8622785
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 75 deletions.
92 changes: 50 additions & 42 deletions lib/onJsonMiddleware.js
@@ -1,5 +1,5 @@
/*
This helper just adds an onJson function to the
This middleware just adds an onJson function to the
req and calls back when done.
As a last or only parameter, the onJson function takes a
Expand All @@ -15,55 +15,63 @@
*/
var JSV = require('JSV').JSV;

var onJsonMiddleware = function(req, res, cb){
req.onJson = function(){

var args = Array.prototype.slice.call(arguments);
var schema, onBodyCB;
switch(args.length){
case 1:
onBodyCB = args[0];
break;

case 2:
schema = args[0];
onBodyCB = args[1];
break;

default : throw "req.onJson() was called with the wrong number of properties.";
}

var body = '';
req.on('data', function(data){
body += data;
});
req.on('error', function(err){
return onBodyCB(err, body);
});
req.on('end', function(){
var obj;
try {
obj = JSON.parse(body);
} catch(ex) {
// if it's not valid JSON...
return res.status.badRequest('invalid json.', body);

var factory = function(onError){


var onJsonMiddleware = function(req, res, cb){
req.onJson = function(){

var args = Array.prototype.slice.call(arguments);
var schema, onBodyCB;
switch(args.length){
case 1:
onBodyCB = args[0];
break;

case 2:
schema = args[0];
onBodyCB = args[1];
break;

default : throw "req.onJson() was called with the wrong number of properties.";
}
if (!!schema){
var report = JSV.createEnvironment().validate(obj, schema);
if (report.errors.length > 0){
return res.status.badRequest({ reason : 'json failed schema validation.', errors : report.errors});

var body = '';
req.on('data', function(data){
body += data;
});
req.on('error', function(err){
return onBodyCB(err, body);
});
req.on('end', function(){
var obj;
try {
obj = JSON.parse(body);
} catch(ex) {
// if it's not valid JSON...
return onError( req, res, { reason : 'invalid json.', detail : body} );
}
if (!!schema){
var report = JSV.createEnvironment().validate(obj, schema);
if (report.errors.length > 0){
return onError(req, res, { reason : 'json failed schema validation.', detail : report.errors});
}
}
}

return onBodyCB(null, obj);
});
return onBodyCB(null, obj);
});

};
cb();
};
cb();

return onJsonMiddleware;

};


module.exports = onJsonMiddleware;
module.exports = factory;



Expand Down
61 changes: 28 additions & 33 deletions test/onJson.js
@@ -1,20 +1,22 @@
var should = require('should');
var jbch = require('../index');
var factory = require('../index');

// TODO test bad schema, successful schema, json parse error
describe("onJsonHelper", function(){
describe("onJson", function(){
it ("sets onJson on the object", function(done){
var req = {};
var res = {};
jbch(req, res, function(){
var middleware = factory(function(req, res, info){ throw "there shouldn't be an error!";});
middleware(req, res, function(){
(typeof req.onJson).should.equal('function');
done();
});
});
it ("throws an error when called with 2+ params", function(done){
var req = {};
var res = {};
jbch(req, res, function(){
var middleware = factory(function(req, res, info){ throw "there shouldn't be an error!";});
middleware(req, res, function(){
try {
req.onJson({}, {}, function(err, obj){
err.should.equal('some error');
Expand All @@ -35,7 +37,8 @@ describe("onJsonHelper", function(){
}
}
};
jbch(req, res, function(){
var middleware = factory(function(req, res, info){ throw "there shouldn't be an error!";});
middleware(req, res, function(){
req.onJson(function(err, obj){
err.should.equal('some error');
done();
Expand All @@ -52,14 +55,16 @@ describe("onJsonHelper", function(){
}
}
};
jbch(req, res, function(){
var middleware = factory(function(req, res, info){ throw "there shouldn't be an error!";});
middleware(req, res, function(){
req.onJson(function(err, obj){
obj.should.eql({asdf:"asdf"});
done();
});
});
});
it ("responds with an error if json doesn't parse", function(done){
var res = {};
var req = {
on : function(type, cb){
switch(type){
Expand All @@ -68,16 +73,12 @@ describe("onJsonHelper", function(){
}
}
};
var res = {
status : {
badRequest : function(message, detail){
message.should.equal('invalid json.');
detail.should.equal('{"age":37,}');
var middleware = factory(function(req, res, info){
info.reason.should.equal('invalid json.');
info.detail.should.equal('{"age":37,}');
done();
}
}
};
jbch(req, res, function(){
});
middleware(req, res, function(){
req.onJson(function(err, obj){
should.fail('should not get here');
});
Expand Down Expand Up @@ -109,11 +110,10 @@ describe("onJsonHelper", function(){
resumeWasCalled = true;
}
};
var res = {
status : {
badRequest : function(error){
error.reason.should.equal('json failed schema validation.');
error.errors[0].message.should.equal('Additional properties are not allowed');
var res = {};
var middleware = factory(function(req, res, info){
info.reason.should.equal('json failed schema validation.');
info.detail[0].message.should.equal('Additional properties are not allowed');
// detail looks like this:
// [ { uri: 'urn:uuid:167293d9-3c95-493d-826e-1bfd4146a8b9#',
// schemaUri: 'urn:uuid:b7e07efd-fd80-4370-8206-9162f4c39cc9#',
Expand All @@ -122,10 +122,8 @@ describe("onJsonHelper", function(){
// details: false } ]
resumeWasCalled = true;
done();
}
}
};
jbch(req, res, function(){
});
middleware(req, res, function(){
req.onJson(schema, function(err, obj){
should.fail('should not get here');
});
Expand All @@ -151,22 +149,19 @@ describe("onJsonHelper", function(){
}
}
};
var res = {
status : {
badRequest : function(error){
error.reason.should.equal('json failed schema validation.');
error.errors[0].details[0].should.equal('number');
var res = {};
var middleware = factory(function(req, res, info){
info.reason.should.equal('json failed schema validation.');
info.detail[0].details[0].should.equal('number');
// detail looks like this:
// [ { uri: 'urn:uuid:67ef53a9-1b09-48b1-b97d-fae313e4ee39#/age',
// schemaUri: 'urn:uuid:51edca59-120a-4486-a961-0ee2aa5c276b#/properties/age',
// attribute: 'type',
// message: 'Instance is not a required type',
// details: [ 'number' ] } ]
done();
}
}
};
jbch(req, res, function(){
});
middleware(req, res, function(){
req.onJson(schema, function(err, obj){
should.fail('should not get here');
});
Expand Down

0 comments on commit 8622785

Please sign in to comment.