From a770ce252d84ddff9f03cb61de4cb2d70a762019 Mon Sep 17 00:00:00 2001 From: Jason Karns Date: Fri, 1 Sep 2017 16:10:49 -0400 Subject: [PATCH] Assimilate statuscode from non-error objects --- index.js | 3 ++- test/test.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6f67206..238d66d 100644 --- a/index.js +++ b/index.js @@ -49,7 +49,7 @@ function createError () { // so much arity going on ~_~ var err var msg - var status = 500 + var status var props = {} for (var i = 0; i < arguments.length; i++) { var arg = arguments[i] @@ -70,6 +70,7 @@ function createError () { break case 'object': props = arg + status = status || props.status || props.statusCode break } } diff --git a/test/test.js b/test/test.js index 70334e3..4f6de2d 100644 --- a/test/test.js +++ b/test/test.js @@ -93,6 +93,30 @@ describe('HTTP Errors', function () { assert.equal(err.id, 1) }) + it('create(props) with status prop', function () { + var err = create({ + id: 1, + status: 418 + }) + assert.equal(err.name, 'ImATeapotError') + assert.equal(err.message, "I'm a teapot") + assert.equal(err.status, 418) + assert.equal(err.statusCode, 418) + assert.equal(err.id, 1) + }) + + it('create(props) with statusCode prop', function () { + var err = create({ + id: 1, + statusCode: 418 + }) + assert.equal(err.name, 'ImATeapotError') + assert.equal(err.message, "I'm a teapot") + assert.equal(err.status, 418) + assert.equal(err.statusCode, 418) + assert.equal(err.id, 1) + }) + it('create(msg, status)', function () { var err = create('LOL', 404) assert.equal(err.name, 'NotFoundError') @@ -153,6 +177,18 @@ describe('HTTP Errors', function () { assert.equal(err.expose, false) }) + it('create(status, err)', function () { + var _err = new Error('LOL') + _err.status = 404 + var err = create(418, _err) + assert.equal(err, _err) + assert.equal(err.name, 'Error') + assert.equal(err.message, 'LOL') + assert.equal(err.status, 404) + assert.equal(err.statusCode, 404) + assert.equal(err.expose, true) + }) + it('create(err, props)', function () { var _err = new Error('LOL') _err.status = 404 @@ -167,10 +203,27 @@ describe('HTTP Errors', function () { assert.equal(err.expose, true) }) + it('create(err, props) with status prop', function () { + var _err = new Error('LOL') + _err.status = 404 + var err = create(_err, { + id: 1, + status: 418 + }) + assert.equal(err.name, 'Error') + assert.equal(err.message, 'LOL') + assert.equal(err.status, 404) + assert.equal(err.statusCode, 404) + assert.equal(err.id, 1) + assert.equal(err.expose, true) + }) + it('create(status, err, props)', function () { var _err = new Error('LOL') - var err = create(404, _err, { - id: 1 + _err.status = 404 + var err = create(418, _err, { + id: 1, + status: 410 }) assert.equal(err, _err) assert.equal(err.name, 'Error')