From 5931714bd8ebf40b8fe450a814eb2885e6f65b42 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 12 Aug 2014 13:19:14 -0700 Subject: [PATCH 1/2] make the second argument to throw properly optional --- lib/context.js | 16 +++++++++------- test/context/throw.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/context.js b/lib/context.js index 2daa816..8151258 100644 --- a/lib/context.js +++ b/lib/context.js @@ -62,27 +62,29 @@ var proto = module.exports = { * this.throw(400, new Error('invalid')); * * @param {String|Number|Error} err, msg or status - * @param {String|Number|Error} err, msg or status + * @param {String|Number|Error} [err, msg or status] * @param {Object} [props] * @api public */ throw: function(msg, status, props){ + if ('object' == typeof status && !(status instanceof Error)) { + props = status; + status = null; + } + if ('number' == typeof msg) { var tmp = msg; msg = status || http.STATUS_CODES[tmp]; status = tmp; } + props = props || {}; + var err = msg instanceof Error ? msg : new Error(msg); - - if (props) { - for (var key in props) err[key] = props[key]; - } - + for (var key in props) err[key] = props[key]; err.status = status || err.status || 500; err.expose = 'number' == typeof err.status && http.STATUS_CODES[err.status] && err.status < 500; - throw err; }, diff --git a/test/context/throw.js b/test/context/throw.js index 158c9ae..3f5d4c0 100644 --- a/test/context/throw.js +++ b/test/context/throw.js @@ -159,3 +159,35 @@ describe('ctx.throw(status, msg, props)', function(){ }) }) }) + +describe('ctx.throw(msg, props)', function(){ + it('should mixin props', function(done){ + var ctx = context(); + + try { + ctx.throw('msg', { prop: true }); + } catch (err) { + assert('msg' == err.message); + assert(500 == err.status); + assert(false === err.expose); + assert(true === err.prop); + done(); + } + }) +}) + +describe('ctx.throw(status, props)', function(){ + it('should mixin props', function(done){ + var ctx = context(); + + try { + ctx.throw(400, { prop: true }); + } catch (err) { + assert('Bad Request' == err.message); + assert(400 == err.status); + assert(true === err.expose); + assert(true === err.prop); + done(); + } + }) +}) From dc0c35471c662d6e2bc86c0412f63f94f98e6c48 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 12 Aug 2014 13:22:33 -0700 Subject: [PATCH 2/2] support errors and update docs --- docs/api/context.md | 3 ++- test/context/throw.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/api/context.md b/docs/api/context.md index 708435e..4cacee3 100644 --- a/docs/api/context.md +++ b/docs/api/context.md @@ -102,10 +102,11 @@ throw err; error messages since you do not want to leak failure details. - You may optionall pass a `properties` object which is merged into the error as-is, useful for decorating machine-friendly errors which are reported to the requester upstream. + You may optionally pass a `properties` object which is merged into the error as-is, useful for decorating machine-friendly errors which are reported to the requester upstream. ```js this.throw(401, 'access_denied', { user: user }); +this.throw('access_denied', { user: user }); ``` ### ctx.respond diff --git a/test/context/throw.js b/test/context/throw.js index 3f5d4c0..8b016a0 100644 --- a/test/context/throw.js +++ b/test/context/throw.js @@ -191,3 +191,19 @@ describe('ctx.throw(status, props)', function(){ } }) }) + +describe('ctx.throw(err, props)', function(){ + it('should mixin props', function(done){ + var ctx = context(); + + try { + ctx.throw(new Error('test'), { prop: true }); + } catch (err) { + assert('test' == err.message); + assert(500 == err.status); + assert(false === err.expose); + assert(true === err.prop); + done(); + } + }) +})