Merge branch 'master' of ssh://github.com/koajs/koa

master
TJ Holowaychuk 2014-08-08 13:31:31 -07:00
commit cbe7ad89d7
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();
}
})
})