From 87c03aff61396237722e0bd4c6cb4fb1891c4818 Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Fri, 20 Dec 2013 15:34:16 -0800 Subject: [PATCH] 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();