From 5c50b6d0b35346156366a8ec1040754e8a59fdef Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 20 Dec 2013 15:32:02 -0800 Subject: [PATCH 1/3] rename error.js to throw.js --- test/context/{error.js => throw.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/context/{error.js => throw.js} (100%) diff --git a/test/context/error.js b/test/context/throw.js similarity index 100% rename from test/context/error.js rename to test/context/throw.js From 87c03aff61396237722e0bd4c6cb4fb1891c4818 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 20 Dec 2013 15:34:16 -0800 Subject: [PATCH 2/3] add support for .throw(status, msg). Closes #130 --- lib/context.js | 8 ++++---- test/context/throw.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) 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/throw.js b/test/context/throw.js index c044565..d3b25a2 100644 --- a/test/context/throw.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(); From 5eb53a7f7a2e12c1dd157b08e9478e5ad923f416 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 20 Dec 2013 15:34:48 -0800 Subject: [PATCH 3/3] add docs for .throw() additions --- docs/api/context.md | 1 + 1 file changed, 1 insertion(+) 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') ```