Skip to content

Commit

Permalink
Remove support for status being non-first argument
Browse files Browse the repository at this point in the history
closes #76
  • Loading branch information
dougwilson committed Nov 15, 2021
1 parent 8c38486 commit 17a3cc3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* Drop support for Node.js 0.6
* Remove `I'mateapot` export; use `ImATeapot` instead
* Remove support for status being non-first argument
* Rename `UnorderedCollection` constructor to `TooEarly`
* deps: depd@2.0.0
- Replace internal `eval` usage with `Function` constructor
Expand Down
26 changes: 10 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,18 @@ function createError () {
var props = {}
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i]
if (arg instanceof Error) {
var type = typeof arg
if (type === 'object' && arg instanceof Error) {
err = arg
status = err.status || err.statusCode || status
continue
}
switch (typeof arg) {
case 'string':
msg = arg
break
case 'number':
status = arg
if (i !== 0) {
deprecate('non-first-argument status code; replace with createError(' + arg + ', ...)')
}
break
case 'object':
props = arg
break
} else if (type === 'number' && i === 0) {
status = arg
} else if (type === 'string') {
msg = arg
} else if (type === 'object') {
props = arg
} else {
throw new TypeError('argument #' + (i + 1) + ' unsupported type ' + type)
}
}

Expand Down
8 changes: 3 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,9 @@ describe('HTTP Errors', function () {
})

it('createError(msg, status)', function () {
var err = createError('LOL', 404)
assert.strictEqual(err.name, 'NotFoundError')
assert.strictEqual(err.message, 'LOL')
assert.strictEqual(err.status, 404)
assert.strictEqual(err.statusCode, 404)
assert.throws(function () {
createError('LOL', 404)
}, /argument #2 unsupported type number/)
})

it('createError(msg)', function () {
Expand Down

0 comments on commit 17a3cc3

Please sign in to comment.