add support for .throw(status, msg). Closes #130

This commit is contained in:
TJ Holowaychuk 2013-12-20 15:34:16 -08:00
parent 5c50b6d0b3
commit 87c03aff61
2 changed files with 18 additions and 4 deletions

View file

@ -44,18 +44,18 @@ module.exports = {
* *
* this.throw(403) * this.throw(403)
* this.throw('name required', 400) * this.throw('name required', 400)
* this.throw(400, 'name required')
* this.throw('something exploded') * this.throw('something exploded')
* *
* @param {String|Number} msg * @param {String|Number} msg or status
* @param {Number} status * @param {String|Number} msg or status
* @api public * @api public
*/ */
throw: function(msg, status){ throw: function(msg, status){
// TODO: switch order... feels weird now that im used to express
if ('number' == typeof msg) { if ('number' == typeof msg) {
var tmp = msg; var tmp = msg;
msg = http.STATUS_CODES[tmp]; msg = status || http.STATUS_CODES[tmp];
status = tmp; status = tmp;
} }

View file

@ -29,6 +29,20 @@ describe('ctx.throw(msg, status)', function(){
}) })
}) })
describe('ctx.throw(status, msg)', function(){
it('should throw an error', function(done){
var ctx = context();
try {
ctx.throw(400, 'name required');
} catch (err) {
assert('name required' == err.message);
assert(400 == err.status);
done();
}
})
})
describe('ctx.throw(status)', function(){ describe('ctx.throw(status)', function(){
it('should throw an error', function(done){ it('should throw an error', function(done){
var ctx = context(); var ctx = context();