Skip to content

Commit f87dc81

Browse files
authored
feat: add captureStackTrace parameter (#145)
* feat: add captureStackTrace parameter * fix name * add tests * change test description
1 parent 334b1e8 commit f87dc81

File tree

5 files changed

+50
-6
lines changed

5 files changed

+50
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ npm i @fastify/error
1616
The module exports a function that you can use for consistent error objects, it takes 4 parameters:
1717

1818
```
19-
createError(code, message [, statusCode [, Base]])
19+
createError(code, message [, statusCode [, Base [, captureStackTrace]]])
2020
```
2121

2222
- `code` (`string`, required) - The error code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `FST_`
2323
- `message` (`string`, required) - The error message. You can also use interpolated strings for formatting the message.
2424
- `statusCode` (`number`, optional) - The status code that Fastify will use if the error is sent via HTTP.
2525
- `Base` (`ErrorConstructor`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)
26+
- `captureStackTrace` (`boolean`, optional) - Whether to capture the stack trace or not.
2627

2728
```js
2829
const createError = require('@fastify/error')

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function toString () {
66
return `${this.name} [${this.code}]: ${this.message}`
77
}
88

9-
function createError (code, message, statusCode = 500, Base = Error) {
9+
function createError (code, message, statusCode = 500, Base = Error, captureStackTrace = createError.captureStackTrace) {
1010
if (!code) throw new Error('Fastify error code must not be empty')
1111
if (!message) throw new Error('Fastify error message must not be empty')
1212

@@ -29,7 +29,7 @@ function createError (code, message, statusCode = 500, Base = Error) {
2929

3030
this.message = format(message, ...args)
3131

32-
Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)
32+
Error.stackTraceLimit && captureStackTrace && Error.captureStackTrace(this, FastifyError)
3333
}
3434

3535
FastifyError.prototype = Object.create(Base.prototype, {
@@ -48,6 +48,8 @@ function createError (code, message, statusCode = 500, Base = Error) {
4848
return FastifyError
4949
}
5050

51+
createError.captureStackTrace = true
52+
5153
module.exports = createError
5254
module.exports.default = createError
5355
module.exports.createError = createError

test/index.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,36 @@ test('Create error with different base', (t) => {
138138
t.assert.ok(err.stack)
139139
})
140140

141+
test('Create error with different base (no stack) (global)', (t) => {
142+
t.plan(7)
143+
144+
createError.captureStackTrace = false
145+
const NewError = createError('CODE', 'hey %s', 500, TypeError)
146+
const err = new NewError('dude')
147+
t.assert.ok(err instanceof Error)
148+
t.assert.ok(err instanceof TypeError)
149+
t.assert.equal(err.name, 'FastifyError')
150+
t.assert.equal(err.message, 'hey dude')
151+
t.assert.equal(err.code, 'CODE')
152+
t.assert.equal(err.statusCode, 500)
153+
t.assert.equal(err.stack, undefined)
154+
createError.captureStackTrace = true
155+
})
156+
157+
test('Create error with different base (no stack) (parameter)', (t) => {
158+
t.plan(7)
159+
160+
const NewError = createError('CODE', 'hey %s', 500, TypeError, false)
161+
const err = new NewError('dude')
162+
t.assert.ok(err instanceof Error)
163+
t.assert.ok(err instanceof TypeError)
164+
t.assert.equal(err.name, 'FastifyError')
165+
t.assert.equal(err.message, 'hey dude')
166+
t.assert.equal(err.code, 'CODE')
167+
t.assert.equal(err.statusCode, 500)
168+
t.assert.equal(err.stack, undefined)
169+
})
170+
141171
test('FastifyError.toString returns code', (t) => {
142172
t.plan(1)
143173

types/index.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ declare function createError<C extends string, SC extends number, Arg extends un
22
code: C,
33
message: string,
44
statusCode: SC,
5-
Base?: ErrorConstructor
5+
Base?: ErrorConstructor,
6+
captureStackTrace?: boolean
67
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>
78

89
declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
910
code: C,
1011
message: string,
1112
statusCode?: number,
12-
Base?: ErrorConstructor
13+
Base?: ErrorConstructor,
14+
captureStackTrace?: boolean
1315
): createError.FastifyErrorConstructor<{ code: C }, Arg>
1416

1517
declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
1618
code: string,
1719
message: string,
1820
statusCode?: number,
19-
Base?: ErrorConstructor
21+
Base?: ErrorConstructor,
22+
captureStackTrace?: boolean
2023
): createError.FastifyErrorConstructor<{ code: string }, Arg>
2124

2225
type CreateError = typeof createError

types/index.test-d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ expectType<'ERROR_CODE'>(err.code)
99
expectType<string>(err.message)
1010
expectType<number | undefined>(err.statusCode)
1111

12+
const CustomErrorNoStackTrace = createError('ERROR_CODE', 'message', undefined, undefined, false)
13+
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomErrorNoStackTrace)
14+
const errNoStackTrace = new CustomErrorNoStackTrace()
15+
expectType<FastifyError & { code: 'ERROR_CODE' }>(errNoStackTrace)
16+
expectType<'ERROR_CODE'>(errNoStackTrace.code)
17+
expectType<string>(errNoStackTrace.message)
18+
expectType<number | undefined>(errNoStackTrace.statusCode)
19+
1220
const CustomTypedError = createError('OTHER_CODE', 'message', 400)
1321
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
1422
const typed = new CustomTypedError()

0 commit comments

Comments
 (0)