Merge pull request #131 from koajs/add/throw-status-msg

Add/throw status msg
This commit is contained in:
TJ Holowaychuk 2013-12-21 10:57:19 -08:00
commit 998b348632
3 changed files with 19 additions and 4 deletions

View File

@ -128,6 +128,7 @@ Note: koa uses the [cookies](https://github.com/jed/cookies) module where option
```js
this.throw(403)
this.throw('name required', 400)
this.throw(400, 'name required')
this.throw('something exploded')
```

View File

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