Skip to content

Commit 7ea98d1

Browse files
dmuharemagicjsumners
authored andcommitted
fix: cause last element index check
1 parent ceb6cde commit 7ea98d1

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function createError (code, message, statusCode = 500, Base = Error) {
2323
this.statusCode = statusCode
2424

2525
const lastElement = args.length - 1
26-
if (lastElement !== 1 && args[lastElement] && typeof args[lastElement] === 'object' && 'cause' in args[lastElement]) {
26+
if (lastElement !== -1 && args[lastElement] && typeof args[lastElement] === 'object' && 'cause' in args[lastElement]) {
2727
this.cause = args.pop().cause
2828
}
2929

test/index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ test('Create an error with cause', t => {
163163
t.equal(err.cause, cause)
164164
})
165165

166+
test('Create an error with cause and message', t => {
167+
t.plan(2)
168+
const cause = new Error('HEY')
169+
const NewError = createError('CODE', 'Not available: %s')
170+
const err = NewError('foo', { cause })
171+
172+
t.ok(err instanceof Error)
173+
t.equal(err.cause, cause)
174+
})
175+
166176
test('Create an error with last argument null', t => {
167177
t.plan(2)
168178
const cause = new Error('HEY')

0 commit comments

Comments
 (0)