diff --git a/index.js b/index.js index 6390315..c4f8375 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,10 @@ module.exports = assert function assert (value, status, msg, opts) { if (value) return - throw createError(status, msg, opts) + var args = [status] + if (msg !== undefined) args.push(msg) + if (opts !== undefined) args.push(opts) + throw createError.apply(null, args) } assert.fail = function (status, msg, opts) { diff --git a/package.json b/package.json index 9625666..21e284d 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "repository": "jshttp/http-assert", "dependencies": { "deep-equal": "~1.0.1", - "http-errors": "~1.8.0" + "http-errors": "~2.0.1" }, "devDependencies": { "eslint": "7.32.0", diff --git a/test/test.js b/test/test.js index acb9f84..f472720 100644 --- a/test/test.js +++ b/test/test.js @@ -36,6 +36,21 @@ describe('assert()', function () { ok(!err.expose) }) + it('should accept options for the error object when msg is undefined', function () { + var err + + try { + assert(false, 401, undefined, { expose: false }) + } catch (e) { + err = e + } + + ok(err) + ok(err.status === 401) + ok(err.message === 'Unauthorized') + ok(!err.expose) + }) + it('should not expose 5xx errors', function () { var err