@fastify/error 3.4.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +1 -1
- package/.github/workflows/ci.yml +4 -2
- package/README.md +5 -4
- package/eslint.config.js +6 -0
- package/index.js +4 -2
- package/package.json +37 -7
- package/test/index.test.js +119 -80
- package/types/index.d.ts +6 -3
- package/types/index.test-d.ts +9 -2
- package/.taprc +0 -2
package/.github/dependabot.yml
CHANGED
package/.github/workflows/ci.yml
CHANGED
|
@@ -4,7 +4,6 @@ on:
|
|
|
4
4
|
push:
|
|
5
5
|
branches:
|
|
6
6
|
- main
|
|
7
|
-
- master
|
|
8
7
|
- next
|
|
9
8
|
- 'v*'
|
|
10
9
|
paths-ignore:
|
|
@@ -17,7 +16,10 @@ on:
|
|
|
17
16
|
|
|
18
17
|
jobs:
|
|
19
18
|
test:
|
|
20
|
-
|
|
19
|
+
permissions:
|
|
20
|
+
contents: write
|
|
21
|
+
pull-requests: write
|
|
22
|
+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
|
|
21
23
|
with:
|
|
22
24
|
license-check: true
|
|
23
25
|
lint: true
|
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @fastify/error
|
|
2
2
|
|
|
3
|
-
](https://github.com/fastify/fastify-error/actions/workflows/ci.yml)
|
|
4
4
|
[](https://www.npmjs.com/package/@fastify/error)
|
|
5
|
-
[](https://github.com/neostandard/neostandard)
|
|
6
6
|
|
|
7
7
|
A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.
|
|
8
8
|
|
|
@@ -16,13 +16,14 @@ npm i @fastify/error
|
|
|
16
16
|
The module exports a function that you can use for consistent error objects, it takes 4 parameters:
|
|
17
17
|
|
|
18
18
|
```
|
|
19
|
-
createError(code, message [, statusCode [, Base]])
|
|
19
|
+
createError(code, message [, statusCode [, Base [, captureStackTrace]]])
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
- `code` (`string`, required) - The error code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `FST_`
|
|
23
23
|
- `message` (`string`, required) - The error message. You can also use interpolated strings for formatting the message.
|
|
24
24
|
- `statusCode` (`number`, optional) - The status code that Fastify will use if the error is sent via HTTP.
|
|
25
25
|
- `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.
|
|
26
27
|
|
|
27
28
|
```js
|
|
28
29
|
const createError = require('@fastify/error')
|
|
@@ -41,7 +42,7 @@ How to add cause:
|
|
|
41
42
|
```js
|
|
42
43
|
const createError = require('@fastify/error')
|
|
43
44
|
const CustomError = createError('ERROR_CODE', 'Hello %s')
|
|
44
|
-
console.log(new CustomError('world', {cause: new Error('cause')}))
|
|
45
|
+
console.log(new CustomError('world', {cause: new Error('cause')}))
|
|
45
46
|
// error.message => 'Hello world'
|
|
46
47
|
// error.cause => Error('cause')
|
|
47
48
|
```
|
package/eslint.config.js
ADDED
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@ function toString () {
|
|
|
6
6
|
return `${this.name} [${this.code}]: ${this.message}`
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
function createError (code, message, statusCode = 500, Base = Error) {
|
|
9
|
+
function createError (code, message, statusCode = 500, Base = Error, captureStackTrace = createError.captureStackTrace) {
|
|
10
10
|
if (!code) throw new Error('Fastify error code must not be empty')
|
|
11
11
|
if (!message) throw new Error('Fastify error message must not be empty')
|
|
12
12
|
|
|
@@ -29,7 +29,7 @@ function createError (code, message, statusCode = 500, Base = Error) {
|
|
|
29
29
|
|
|
30
30
|
this.message = format(message, ...args)
|
|
31
31
|
|
|
32
|
-
Error.stackTraceLimit
|
|
32
|
+
Error.stackTraceLimit && captureStackTrace && Error.captureStackTrace(this, FastifyError)
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
FastifyError.prototype = Object.create(Base.prototype, {
|
|
@@ -48,6 +48,8 @@ function createError (code, message, statusCode = 500, Base = Error) {
|
|
|
48
48
|
return FastifyError
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
createError.captureStackTrace = true
|
|
52
|
+
|
|
51
53
|
module.exports = createError
|
|
52
54
|
module.exports.default = createError
|
|
53
55
|
module.exports.createError = createError
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/error",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"description": "A small utility, used by Fastify itself, for generating consistent error objects across your codebase and plugins.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
7
7
|
"types": "types/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"lint": "
|
|
10
|
-
"lint:fix": "
|
|
9
|
+
"lint": "eslint",
|
|
10
|
+
"lint:fix": "eslint --fix",
|
|
11
11
|
"test": "npm run test:unit && npm run test:typescript",
|
|
12
|
-
"test:unit": "
|
|
12
|
+
"test:unit": "c8 --100 node --test",
|
|
13
13
|
"test:typescript": "tsd"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
@@ -23,16 +23,46 @@
|
|
|
23
23
|
"plugin"
|
|
24
24
|
],
|
|
25
25
|
"author": "Tomas Della Vedova",
|
|
26
|
+
"contributors": [
|
|
27
|
+
{
|
|
28
|
+
"name": "Matteo Collina",
|
|
29
|
+
"email": "hello@matteocollina.com"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"name": "James Sumners",
|
|
33
|
+
"url": "https://james.sumners.info"
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"name": "Aras Abbasi",
|
|
37
|
+
"email": "aras.abbasi@gmail.com"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"name": "Frazer Smith",
|
|
41
|
+
"email": "frazer.dev@icloud.com",
|
|
42
|
+
"url": "https://github.com/fdawgs"
|
|
43
|
+
}
|
|
44
|
+
],
|
|
26
45
|
"license": "MIT",
|
|
27
46
|
"bugs": {
|
|
28
47
|
"url": "https://github.com/fastify/fastify-error/issues"
|
|
29
48
|
},
|
|
30
49
|
"homepage": "https://github.com/fastify/fastify-error#readme",
|
|
50
|
+
"funding": [
|
|
51
|
+
{
|
|
52
|
+
"type": "github",
|
|
53
|
+
"url": "https://github.com/sponsors/fastify"
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"type": "opencollective",
|
|
57
|
+
"url": "https://opencollective.com/fastify"
|
|
58
|
+
}
|
|
59
|
+
],
|
|
31
60
|
"devDependencies": {
|
|
32
61
|
"benchmark": "^2.1.4",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
62
|
+
"c8": "^10.1.2",
|
|
63
|
+
"eslint": "^9.17.0",
|
|
64
|
+
"neostandard": "^0.12.0",
|
|
65
|
+
"tsd": "^0.31.0"
|
|
36
66
|
},
|
|
37
67
|
"tsd": {
|
|
38
68
|
"compilerOptions": {
|
package/test/index.test.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const test = require('node:test')
|
|
4
4
|
const createError = require('..')
|
|
5
5
|
|
|
6
|
-
test('Create error with zero parameter', t => {
|
|
6
|
+
test('Create error with zero parameter', (t) => {
|
|
7
7
|
t.plan(6)
|
|
8
8
|
|
|
9
9
|
const NewError = createError('CODE', 'Not available')
|
|
10
10
|
const err = new NewError()
|
|
11
|
-
t.ok(err instanceof Error)
|
|
12
|
-
t.equal(err.name, 'FastifyError')
|
|
13
|
-
t.equal(err.message, 'Not available')
|
|
14
|
-
t.equal(err.code, 'CODE')
|
|
15
|
-
t.equal(err.statusCode, 500)
|
|
16
|
-
t.ok(err.stack)
|
|
11
|
+
t.assert.ok(err instanceof Error)
|
|
12
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
13
|
+
t.assert.equal(err.message, 'Not available')
|
|
14
|
+
t.assert.equal(err.code, 'CODE')
|
|
15
|
+
t.assert.equal(err.statusCode, 500)
|
|
16
|
+
t.assert.ok(err.stack)
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
test('Create error with 1 parameter', t => {
|
|
19
|
+
test('Create error with 1 parameter', (t) => {
|
|
20
20
|
t.plan(6)
|
|
21
21
|
|
|
22
22
|
const NewError = createError('CODE', 'hey %s')
|
|
23
23
|
const err = new NewError('alice')
|
|
24
|
-
t.
|
|
25
|
-
t.
|
|
26
|
-
t.equal(err.message, 'hey alice')
|
|
27
|
-
t.equal(err.code, 'CODE')
|
|
28
|
-
t.equal(err.statusCode, 500)
|
|
29
|
-
t.ok(err.stack)
|
|
24
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
25
|
+
t.assert.ok(err instanceof Error)
|
|
26
|
+
t.assert.equal(err.message, 'hey alice')
|
|
27
|
+
t.assert.equal(err.code, 'CODE')
|
|
28
|
+
t.assert.equal(err.statusCode, 500)
|
|
29
|
+
t.assert.ok(err.stack)
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
-
test('Create error with 1 parameter set to undefined', t => {
|
|
32
|
+
test('Create error with 1 parameter set to undefined', (t) => {
|
|
33
33
|
t.plan(1)
|
|
34
34
|
|
|
35
35
|
const NewError = createError('CODE', 'hey %s')
|
|
36
36
|
const err = new NewError(undefined)
|
|
37
|
-
t.equal(err.message, 'hey undefined')
|
|
37
|
+
t.assert.equal(err.message, 'hey undefined')
|
|
38
38
|
})
|
|
39
39
|
|
|
40
40
|
test('Create error with 2 parameters', (t) => {
|
|
@@ -42,143 +42,182 @@ test('Create error with 2 parameters', (t) => {
|
|
|
42
42
|
|
|
43
43
|
const NewError = createError('CODE', 'hey %s, I like your %s')
|
|
44
44
|
const err = new NewError('alice', 'attitude')
|
|
45
|
-
t.ok(err instanceof Error)
|
|
46
|
-
t.equal(err.name, 'FastifyError')
|
|
47
|
-
t.equal(err.message, 'hey alice, I like your attitude')
|
|
48
|
-
t.equal(err.code, 'CODE')
|
|
49
|
-
t.equal(err.statusCode, 500)
|
|
50
|
-
t.ok(err.stack)
|
|
45
|
+
t.assert.ok(err instanceof Error)
|
|
46
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
47
|
+
t.assert.equal(err.message, 'hey alice, I like your attitude')
|
|
48
|
+
t.assert.equal(err.code, 'CODE')
|
|
49
|
+
t.assert.equal(err.statusCode, 500)
|
|
50
|
+
t.assert.ok(err.stack)
|
|
51
51
|
})
|
|
52
52
|
|
|
53
|
-
test('Create error with 2 parameters set to undefined', t => {
|
|
53
|
+
test('Create error with 2 parameters set to undefined', (t) => {
|
|
54
54
|
t.plan(1)
|
|
55
55
|
|
|
56
56
|
const NewError = createError('CODE', 'hey %s, I like your %s')
|
|
57
57
|
const err = new NewError(undefined, undefined)
|
|
58
|
-
t.equal(err.message, 'hey undefined, I like your undefined')
|
|
58
|
+
t.assert.equal(err.message, 'hey undefined, I like your undefined')
|
|
59
59
|
})
|
|
60
60
|
|
|
61
|
-
test('Create error with 3 parameters', t => {
|
|
61
|
+
test('Create error with 3 parameters', (t) => {
|
|
62
62
|
t.plan(6)
|
|
63
63
|
|
|
64
64
|
const NewError = createError('CODE', 'hey %s, I like your %s %s')
|
|
65
65
|
const err = new NewError('alice', 'attitude', 'see you')
|
|
66
|
-
t.ok(err instanceof Error)
|
|
67
|
-
t.equal(err.name, 'FastifyError')
|
|
68
|
-
t.equal(err.message, 'hey alice, I like your attitude see you')
|
|
69
|
-
t.equal(err.code, 'CODE')
|
|
70
|
-
t.equal(err.statusCode, 500)
|
|
71
|
-
t.ok(err.stack)
|
|
66
|
+
t.assert.ok(err instanceof Error)
|
|
67
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
68
|
+
t.assert.equal(err.message, 'hey alice, I like your attitude see you')
|
|
69
|
+
t.assert.equal(err.code, 'CODE')
|
|
70
|
+
t.assert.equal(err.statusCode, 500)
|
|
71
|
+
t.assert.ok(err.stack)
|
|
72
72
|
})
|
|
73
73
|
|
|
74
|
-
test('Create error with 3 parameters set to undefined', t => {
|
|
74
|
+
test('Create error with 3 parameters set to undefined', (t) => {
|
|
75
75
|
t.plan(4)
|
|
76
76
|
|
|
77
77
|
const NewError = createError('CODE', 'hey %s, I like your %s %s')
|
|
78
78
|
const err = new NewError(undefined, undefined, undefined)
|
|
79
|
-
t.equal(err.message, 'hey undefined, I like your undefined undefined')
|
|
80
|
-
t.equal(err.code, 'CODE')
|
|
81
|
-
t.equal(err.statusCode, 500)
|
|
82
|
-
t.ok(err.stack)
|
|
79
|
+
t.assert.equal(err.message, 'hey undefined, I like your undefined undefined')
|
|
80
|
+
t.assert.equal(err.code, 'CODE')
|
|
81
|
+
t.assert.equal(err.statusCode, 500)
|
|
82
|
+
t.assert.ok(err.stack)
|
|
83
83
|
})
|
|
84
84
|
|
|
85
|
-
test('Create error with 4 parameters set to undefined', t => {
|
|
85
|
+
test('Create error with 4 parameters set to undefined', (t) => {
|
|
86
86
|
t.plan(4)
|
|
87
87
|
|
|
88
88
|
const NewError = createError('CODE', 'hey %s, I like your %s %s and %s')
|
|
89
89
|
const err = new NewError(undefined, undefined, undefined, undefined)
|
|
90
|
-
t.equal(
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
90
|
+
t.assert.equal(
|
|
91
|
+
err.message,
|
|
92
|
+
'hey undefined, I like your undefined undefined and undefined'
|
|
93
|
+
)
|
|
94
|
+
t.assert.equal(err.code, 'CODE')
|
|
95
|
+
t.assert.equal(err.statusCode, 500)
|
|
96
|
+
t.assert.ok(err.stack)
|
|
94
97
|
})
|
|
95
98
|
|
|
96
|
-
test('Create error with no statusCode property', t => {
|
|
99
|
+
test('Create error with no statusCode property', (t) => {
|
|
97
100
|
t.plan(6)
|
|
98
101
|
|
|
99
102
|
const NewError = createError('CODE', 'hey %s', 0)
|
|
100
103
|
const err = new NewError('dude')
|
|
101
|
-
t.ok(err instanceof Error)
|
|
102
|
-
t.equal(err.name, 'FastifyError')
|
|
103
|
-
t.equal(err.message, 'hey dude')
|
|
104
|
-
t.equal(err.code, 'CODE')
|
|
105
|
-
t.equal(err.statusCode, undefined)
|
|
106
|
-
t.ok(err.stack)
|
|
104
|
+
t.assert.ok(err instanceof Error)
|
|
105
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
106
|
+
t.assert.equal(err.message, 'hey dude')
|
|
107
|
+
t.assert.equal(err.code, 'CODE')
|
|
108
|
+
t.assert.equal(err.statusCode, undefined)
|
|
109
|
+
t.assert.ok(err.stack)
|
|
107
110
|
})
|
|
108
111
|
|
|
109
|
-
test('Should throw when error code has no fastify code', t => {
|
|
112
|
+
test('Should throw when error code has no fastify code', (t) => {
|
|
110
113
|
t.plan(1)
|
|
114
|
+
t.assert.throws(
|
|
115
|
+
() => createError(),
|
|
116
|
+
new Error('Fastify error code must not be empty')
|
|
117
|
+
)
|
|
118
|
+
})
|
|
111
119
|
|
|
112
|
-
|
|
120
|
+
test('Should throw when error code has no message', (t) => {
|
|
121
|
+
t.assert.throws(
|
|
122
|
+
() => createError('code'),
|
|
123
|
+
new Error('Fastify error message must not be empty')
|
|
124
|
+
)
|
|
113
125
|
})
|
|
114
126
|
|
|
115
|
-
test('
|
|
116
|
-
t.plan(
|
|
127
|
+
test('Create error with different base', (t) => {
|
|
128
|
+
t.plan(7)
|
|
117
129
|
|
|
118
|
-
|
|
130
|
+
const NewError = createError('CODE', 'hey %s', 500, TypeError)
|
|
131
|
+
const err = new NewError('dude')
|
|
132
|
+
t.assert.ok(err instanceof Error)
|
|
133
|
+
t.assert.ok(err instanceof TypeError)
|
|
134
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
135
|
+
t.assert.equal(err.message, 'hey dude')
|
|
136
|
+
t.assert.equal(err.code, 'CODE')
|
|
137
|
+
t.assert.equal(err.statusCode, 500)
|
|
138
|
+
t.assert.ok(err.stack)
|
|
119
139
|
})
|
|
120
140
|
|
|
121
|
-
test('Create error with different base', t => {
|
|
141
|
+
test('Create error with different base (no stack) (global)', (t) => {
|
|
122
142
|
t.plan(7)
|
|
123
143
|
|
|
144
|
+
createError.captureStackTrace = false
|
|
124
145
|
const NewError = createError('CODE', 'hey %s', 500, TypeError)
|
|
125
146
|
const err = new NewError('dude')
|
|
126
|
-
t.ok(err instanceof Error)
|
|
127
|
-
t.ok(err instanceof TypeError)
|
|
128
|
-
t.equal(err.name, 'FastifyError')
|
|
129
|
-
t.equal(err.message, 'hey dude')
|
|
130
|
-
t.equal(err.code, 'CODE')
|
|
131
|
-
t.equal(err.statusCode, 500)
|
|
132
|
-
t.
|
|
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
|
|
133
155
|
})
|
|
134
156
|
|
|
135
|
-
test('
|
|
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
|
+
|
|
171
|
+
test('FastifyError.toString returns code', (t) => {
|
|
136
172
|
t.plan(1)
|
|
137
173
|
|
|
138
174
|
const NewError = createError('CODE', 'foo')
|
|
139
175
|
const err = new NewError()
|
|
140
|
-
t.equal(err.toString(), 'FastifyError [CODE]: foo')
|
|
176
|
+
t.assert.equal(err.toString(), 'FastifyError [CODE]: foo')
|
|
141
177
|
})
|
|
142
178
|
|
|
143
|
-
test('Create the error without the new keyword', t => {
|
|
179
|
+
test('Create the error without the new keyword', (t) => {
|
|
144
180
|
t.plan(6)
|
|
145
181
|
|
|
146
182
|
const NewError = createError('CODE', 'Not available')
|
|
147
183
|
const err = NewError()
|
|
148
|
-
t.ok(err instanceof Error)
|
|
149
|
-
t.equal(err.name, 'FastifyError')
|
|
150
|
-
t.equal(err.message, 'Not available')
|
|
151
|
-
t.equal(err.code, 'CODE')
|
|
152
|
-
t.equal(err.statusCode, 500)
|
|
153
|
-
t.ok(err.stack)
|
|
184
|
+
t.assert.ok(err instanceof Error)
|
|
185
|
+
t.assert.equal(err.name, 'FastifyError')
|
|
186
|
+
t.assert.equal(err.message, 'Not available')
|
|
187
|
+
t.assert.equal(err.code, 'CODE')
|
|
188
|
+
t.assert.equal(err.statusCode, 500)
|
|
189
|
+
t.assert.ok(err.stack)
|
|
154
190
|
})
|
|
155
191
|
|
|
156
|
-
test('Create an error with cause', t => {
|
|
192
|
+
test('Create an error with cause', (t) => {
|
|
157
193
|
t.plan(2)
|
|
194
|
+
|
|
158
195
|
const cause = new Error('HEY')
|
|
159
196
|
const NewError = createError('CODE', 'Not available')
|
|
160
197
|
const err = NewError({ cause })
|
|
161
198
|
|
|
162
|
-
t.ok(err instanceof Error)
|
|
163
|
-
t.equal(err.cause, cause)
|
|
199
|
+
t.assert.ok(err instanceof Error)
|
|
200
|
+
t.assert.equal(err.cause, cause)
|
|
164
201
|
})
|
|
165
202
|
|
|
166
|
-
test('Create an error with cause and message', t => {
|
|
203
|
+
test('Create an error with cause and message', (t) => {
|
|
167
204
|
t.plan(2)
|
|
205
|
+
|
|
168
206
|
const cause = new Error('HEY')
|
|
169
207
|
const NewError = createError('CODE', 'Not available: %s')
|
|
170
208
|
const err = NewError('foo', { cause })
|
|
171
209
|
|
|
172
|
-
t.ok(err instanceof Error)
|
|
173
|
-
t.equal(err.cause, cause)
|
|
210
|
+
t.assert.ok(err instanceof Error)
|
|
211
|
+
t.assert.equal(err.cause, cause)
|
|
174
212
|
})
|
|
175
213
|
|
|
176
|
-
test('Create an error with last argument null', t => {
|
|
214
|
+
test('Create an error with last argument null', (t) => {
|
|
177
215
|
t.plan(2)
|
|
216
|
+
|
|
178
217
|
const cause = new Error('HEY')
|
|
179
218
|
const NewError = createError('CODE', 'Not available')
|
|
180
219
|
const err = NewError({ cause }, null)
|
|
181
220
|
|
|
182
|
-
t.ok(err instanceof Error)
|
|
183
|
-
t.
|
|
221
|
+
t.assert.ok(err instanceof Error)
|
|
222
|
+
t.assert.ifError(err.cause)
|
|
184
223
|
})
|
package/types/index.d.ts
CHANGED
|
@@ -2,21 +2,24 @@ declare function createError<C extends string, SC extends number, Arg extends un
|
|
|
2
2
|
code: C,
|
|
3
3
|
message: string,
|
|
4
4
|
statusCode: SC,
|
|
5
|
-
Base?: ErrorConstructor
|
|
5
|
+
Base?: ErrorConstructor,
|
|
6
|
+
captureStackTrace?: boolean
|
|
6
7
|
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>
|
|
7
8
|
|
|
8
9
|
declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
|
|
9
10
|
code: C,
|
|
10
11
|
message: string,
|
|
11
12
|
statusCode?: number,
|
|
12
|
-
Base?: ErrorConstructor
|
|
13
|
+
Base?: ErrorConstructor,
|
|
14
|
+
captureStackTrace?: boolean
|
|
13
15
|
): createError.FastifyErrorConstructor<{ code: C }, Arg>
|
|
14
16
|
|
|
15
17
|
declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
|
|
16
18
|
code: string,
|
|
17
19
|
message: string,
|
|
18
20
|
statusCode?: number,
|
|
19
|
-
Base?: ErrorConstructor
|
|
21
|
+
Base?: ErrorConstructor,
|
|
22
|
+
captureStackTrace?: boolean
|
|
20
23
|
): createError.FastifyErrorConstructor<{ code: string }, Arg>
|
|
21
24
|
|
|
22
25
|
type CreateError = typeof createError
|
package/types/index.test-d.ts
CHANGED
|
@@ -9,6 +9,14 @@ expectType<'ERROR_CODE'>(err.code)
|
|
|
9
9
|
expectType<string>(err.message)
|
|
10
10
|
expectType<number | undefined>(err.statusCode)
|
|
11
11
|
|
|
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
|
+
|
|
12
20
|
const CustomTypedError = createError('OTHER_CODE', 'message', 400)
|
|
13
21
|
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
|
|
14
22
|
const typed = new CustomTypedError()
|
|
@@ -66,7 +74,6 @@ expectError(new CustomTypedArgError6('a', 'b', 'c'))
|
|
|
66
74
|
CustomTypedArgError6('a', 'b', 'c', 'd')
|
|
67
75
|
expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))
|
|
68
76
|
|
|
69
|
-
|
|
70
77
|
const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError)
|
|
71
78
|
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor)
|
|
72
|
-
CustomErrorWithErrorConstructor({cause: new Error('Error')})
|
|
79
|
+
CustomErrorWithErrorConstructor({ cause: new Error('Error') })
|
package/.taprc
DELETED