Skip to content

Commit ad99cd0

Browse files
DanieleFedeliDaniele Fedeli
andauthored
Added support for cause (#116)
* Added support for cause * Removed console.error * Added support for cause w/o changing signature * Removed hasCause from types * Removed an extra hasCause * Removed extra comma * Documentation change * Fix for node v14 * Applied lint fix --------- Co-authored-by: Daniele Fedeli <danielefedeli@MacBook-Air.station>
1 parent a323532 commit ad99cd0

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ const CustomError = createError('ERROR_CODE', 'Hello %s')
3737
console.log(new CustomError('world')) // error.message => 'Hello world'
3838
```
3939

40+
How to add cause:
41+
```js
42+
const createError = require('@fastify/error')
43+
const CustomError = createError('ERROR_CODE', 'Hello %s')
44+
console.log(new CustomError('world', {cause: new Error('cause')}))
45+
// error.message => 'Hello world'
46+
// error.cause => Error('cause')
47+
```
48+
4049
### TypeScript
4150

4251
It is possible to limit your error constructor with a generic type using TypeScript:

benchmarks/instantiate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const FastifyError = createError('CODE', 'Not available')
77
const FastifyError1 = createError('CODE', 'Not %s available')
88
const FastifyError2 = createError('CODE', 'Not %s available %s')
99

10+
const cause = new Error('cause')
1011
new benchmark.Suite()
1112
.add('instantiate Error', function () { new Error() }, { minSamples: 100 }) // eslint-disable-line no-new
1213
.add('instantiate FastifyError', function () { new FastifyError() }, { minSamples: 100 }) // eslint-disable-line no-new
1314
.add('instantiate FastifyError arg 1', function () { new FastifyError1('q') }, { minSamples: 100 }) // eslint-disable-line no-new
1415
.add('instantiate FastifyError arg 2', function () { new FastifyError2('qq', 'ss') }, { minSamples: 100 }) // eslint-disable-line no-new
16+
.add('instantiate FastifyError cause', function () { new FastifyError2({ cause }) }, { minSamples: 100 }) // eslint-disable-line no-new
1517
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
1618
.run({ async: false })

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ function createError (code, message, statusCode = 500, Base = Error) {
1717
if (!new.target) {
1818
return new FastifyError(...args)
1919
}
20+
2021
this.code = code
2122
this.name = 'FastifyError'
2223
this.statusCode = statusCode
2324

25+
const lastElement = args.length - 1
26+
if (lastElement !== 1 && args[lastElement] && typeof args[lastElement] === 'object' && 'cause' in args[lastElement]) {
27+
this.cause = args.pop().cause
28+
}
29+
2430
this.message = format(message, ...args)
2531

2632
Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)

test/index.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,23 @@ test('Create the error without the new keyword', t => {
152152
t.equal(err.statusCode, 500)
153153
t.ok(err.stack)
154154
})
155+
156+
test('Create an error with cause', t => {
157+
t.plan(2)
158+
const cause = new Error('HEY')
159+
const NewError = createError('CODE', 'Not available')
160+
const err = NewError({ cause })
161+
162+
t.ok(err instanceof Error)
163+
t.equal(err.cause, cause)
164+
})
165+
166+
test('Create an error with last argument null', t => {
167+
t.plan(2)
168+
const cause = new Error('HEY')
169+
const NewError = createError('CODE', 'Not available')
170+
const err = NewError({ cause }, null)
171+
172+
t.ok(err instanceof Error)
173+
t.notOk(err.cause)
174+
})

types/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,4 @@ expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))
6969

7070
const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError)
7171
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor)
72+
CustomErrorWithErrorConstructor({cause: new Error('Error')})

0 commit comments

Comments
 (0)