Merge pull request #332 from ianstormtaylor/add/throw-props

add the ability to pass `props` to `context.throw`
This commit is contained in:
TJ Holowaychuk 2014-08-08 13:29:32 -07:00
commit 7986756e28
2 changed files with 23 additions and 1 deletions

View file

@ -63,10 +63,11 @@ var proto = module.exports = {
*
* @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){
throw: function(msg, status, props){
if ('number' == typeof msg) {
var tmp = msg;
msg = status || http.STATUS_CODES[tmp];
@ -76,6 +77,11 @@ var proto = module.exports = {
var err = msg instanceof Error ? msg : new Error(msg);
err.status = status || err.status || 500;
err.expose = 'number' == typeof err.status && http.STATUS_CODES[err.status] && err.status < 500;
if (props) {
for (var key in props) err[key] = props[key];
}
throw err;
},

View file

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