diff --git a/docs/api/context.md b/docs/api/context.md index 4efb3d0..3e96397 100644 --- a/docs/api/context.md +++ b/docs/api/context.md @@ -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') ``` diff --git a/lib/context.js b/lib/context.js index 050ea1a..4477c69 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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; } diff --git a/test/context/error.js b/test/context/throw.js similarity index 74% rename from test/context/error.js rename to test/context/throw.js index c044565..d3b25a2 100644 --- a/test/context/error.js +++ b/test/context/throw.js @@ -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();