From bcac468f7c84d75cca0757d7e5aa1948a65c7709 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Fri, 8 Aug 2014 12:37:04 -0700 Subject: [PATCH] add the ability to pass `props` to `context.throw` --- lib/context.js | 8 +++++++- test/context/throw.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/context.js b/lib/context.js index b5b7c81..0c3ea57 100644 --- a/lib/context.js +++ b/lib/context.js @@ -63,10 +63,11 @@ var proto = module.exports = { * * @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){ + throw: function(msg, status, props){ if ('number' == typeof msg) { var tmp = msg; msg = status || http.STATUS_CODES[tmp]; @@ -76,6 +77,11 @@ var proto = module.exports = { var err = msg instanceof Error ? msg : new Error(msg); err.status = status || err.status || 500; err.expose = 'number' == typeof err.status && http.STATUS_CODES[err.status] && err.status < 500; + + if (props) { + for (var key in props) err[key] = props[key]; + } + throw err; }, diff --git a/test/context/throw.js b/test/context/throw.js index ce332ac..7c49259 100644 --- a/test/context/throw.js +++ b/test/context/throw.js @@ -124,3 +124,19 @@ describe('ctx.throw(status)', function(){ }) }) }) + +describe('ctx.throw(status, msg, props)', function(){ + it('should mixin props', function(done){ + var ctx = context(); + + try { + ctx.throw(400, 'msg', { prop: true }); + } catch (err) { + assert('msg' == err.message); + assert(400 == err.status); + assert(true === err.expose); + assert(true === err.prop); + done(); + } + }) +}) \ No newline at end of file