make the second argument to throw properly optional

This commit is contained in:
Ian Storm Taylor 2014-08-12 13:19:14 -07:00
parent 70c72df110
commit 5931714bd8
2 changed files with 41 additions and 7 deletions

View file

@ -62,27 +62,29 @@ var proto = module.exports = {
* this.throw(400, new Error('invalid'));
*
* @param {String|Number|Error} err, msg or status
* @param {String|Number|Error} err, msg or status
* @param {String|Number|Error} [err, msg or status]
* @param {Object} [props]
* @api public
*/
throw: function(msg, status, props){
if ('object' == typeof status && !(status instanceof Error)) {
props = status;
status = null;
}
if ('number' == typeof msg) {
var tmp = msg;
msg = status || http.STATUS_CODES[tmp];
status = tmp;
}
props = props || {};
var err = msg instanceof Error ? msg : new Error(msg);
if (props) {
for (var key in props) err[key] = props[key];
}
for (var key in props) err[key] = props[key];
err.status = status || err.status || 500;
err.expose = 'number' == typeof err.status && http.STATUS_CODES[err.status] && err.status < 500;
throw err;
},

View file

@ -159,3 +159,35 @@ describe('ctx.throw(status, msg, props)', function(){
})
})
})
describe('ctx.throw(msg, props)', function(){
it('should mixin props', function(done){
var ctx = context();
try {
ctx.throw('msg', { prop: true });
} catch (err) {
assert('msg' == err.message);
assert(500 == err.status);
assert(false === err.expose);
assert(true === err.prop);
done();
}
})
})
describe('ctx.throw(status, props)', function(){
it('should mixin props', function(done){
var ctx = context();
try {
ctx.throw(400, { prop: true });
} catch (err) {
assert('Bad Request' == err.message);
assert(400 == err.status);
assert(true === err.expose);
assert(true === err.prop);
done();
}
})
})