@fastify/error 3.2.0 → 3.2.1
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 +0 -0
- package/.github/workflows/ci.yml +5 -0
- package/.taprc +0 -0
- package/LICENSE +0 -0
- package/README.md +13 -0
- package/benchmarks/create.js +9 -0
- package/benchmarks/instantiate.js +16 -0
- package/benchmarks/no-stack.js +13 -0
- package/benchmarks/toString.js +11 -0
- package/index.js +8 -22
- package/package.json +4 -2
- package/test/index.test.js +0 -0
- package/types/index.d.ts +31 -10
- package/types/index.test-d.ts +53 -4
package/.github/dependabot.yml
CHANGED
|
File without changes
|
package/.github/workflows/ci.yml
CHANGED
package/.taprc
CHANGED
|
File without changes
|
package/LICENSE
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
@@ -37,6 +37,19 @@ const CustomError = createError('ERROR_CODE', 'Hello %s')
|
|
|
37
37
|
console.log(new CustomError('world')) // error.message => 'Hello world'
|
|
38
38
|
```
|
|
39
39
|
|
|
40
|
+
### TypeScript
|
|
41
|
+
|
|
42
|
+
It is possible to limit your error constructor with a generic type using TypeScript:
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
const CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')
|
|
46
|
+
new CustomError('world')
|
|
47
|
+
//@ts-expect-error
|
|
48
|
+
new CustomError(1)
|
|
49
|
+
//@ts-expect-error
|
|
50
|
+
new CustomError(1)
|
|
51
|
+
```
|
|
52
|
+
|
|
40
53
|
## License
|
|
41
54
|
|
|
42
55
|
Licensed under [MIT](./LICENSE).
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const benchmark = require('benchmark')
|
|
4
|
+
const createError = require('..')
|
|
5
|
+
|
|
6
|
+
new benchmark.Suite()
|
|
7
|
+
.add('create FastifyError', function () { createError('CODE', 'Not available') }, { minSamples: 100 })
|
|
8
|
+
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
|
|
9
|
+
.run({ async: false })
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const benchmark = require('benchmark')
|
|
4
|
+
const createError = require('..')
|
|
5
|
+
|
|
6
|
+
const FastifyError = createError('CODE', 'Not available')
|
|
7
|
+
const FastifyError1 = createError('CODE', 'Not %s available')
|
|
8
|
+
const FastifyError2 = createError('CODE', 'Not %s available %s')
|
|
9
|
+
|
|
10
|
+
new benchmark.Suite()
|
|
11
|
+
.add('instantiate Error', function () { new Error() }, { minSamples: 100 }) // eslint-disable-line no-new
|
|
12
|
+
.add('instantiate FastifyError', function () { new FastifyError() }, { minSamples: 100 }) // eslint-disable-line no-new
|
|
13
|
+
.add('instantiate FastifyError arg 1', function () { new FastifyError1('q') }, { minSamples: 100 }) // eslint-disable-line no-new
|
|
14
|
+
.add('instantiate FastifyError arg 2', function () { new FastifyError2('qq', 'ss') }, { minSamples: 100 }) // eslint-disable-line no-new
|
|
15
|
+
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
|
|
16
|
+
.run({ async: false })
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const benchmark = require('benchmark')
|
|
4
|
+
const createError = require('..')
|
|
5
|
+
|
|
6
|
+
const FastifyError = createError('CODE', 'Not available')
|
|
7
|
+
Error.stackTraceLimit = 0
|
|
8
|
+
|
|
9
|
+
new benchmark.Suite()
|
|
10
|
+
.add('no-stack instantiate Error', function () { new Error() }, { minSamples: 100 }) // eslint-disable-line no-new
|
|
11
|
+
.add('no-stack instantiate FastifyError', function () { new FastifyError() }, { minSamples: 100 }) // eslint-disable-line no-new
|
|
12
|
+
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
|
|
13
|
+
.run({ async: false })
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const benchmark = require('benchmark')
|
|
4
|
+
const createError = require('..')
|
|
5
|
+
|
|
6
|
+
const FastifyError = createError('CODE', 'Not available')
|
|
7
|
+
|
|
8
|
+
new benchmark.Suite()
|
|
9
|
+
.add('FastifyError toString', function () { new FastifyError().toString() }, { minSamples: 100 })
|
|
10
|
+
.on('cycle', function onCycle (event) { console.log(String(event.target)) })
|
|
11
|
+
.run({ async: false })
|
package/index.js
CHANGED
|
@@ -7,35 +7,21 @@ function createError (code, message, statusCode = 500, Base = Error) {
|
|
|
7
7
|
if (!message) throw new Error('Fastify error message must not be empty')
|
|
8
8
|
|
|
9
9
|
code = code.toUpperCase()
|
|
10
|
+
!statusCode && (statusCode = undefined)
|
|
10
11
|
|
|
11
|
-
function FastifyError (
|
|
12
|
+
function FastifyError (...args) {
|
|
12
13
|
if (!new.target) {
|
|
13
|
-
return new FastifyError(...
|
|
14
|
+
return new FastifyError(...args)
|
|
14
15
|
}
|
|
15
|
-
Error.captureStackTrace(this, FastifyError)
|
|
16
|
-
this.name = 'FastifyError'
|
|
17
16
|
this.code = code
|
|
17
|
+
this.name = 'FastifyError'
|
|
18
|
+
this.statusCode = statusCode
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
switch (arguments.length) {
|
|
21
|
-
case 3:
|
|
22
|
-
this.message = format(message, a, b, c)
|
|
23
|
-
break
|
|
24
|
-
case 2:
|
|
25
|
-
this.message = format(message, a, b)
|
|
26
|
-
break
|
|
27
|
-
case 1:
|
|
28
|
-
this.message = format(message, a)
|
|
29
|
-
break
|
|
30
|
-
case 0:
|
|
31
|
-
this.message = message
|
|
32
|
-
break
|
|
33
|
-
default:
|
|
34
|
-
this.message = format(message, ...arguments)
|
|
35
|
-
}
|
|
20
|
+
this.message = format(message, ...args)
|
|
36
21
|
|
|
37
|
-
|
|
22
|
+
Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)
|
|
38
23
|
}
|
|
24
|
+
|
|
39
25
|
FastifyError.prototype[Symbol.toStringTag] = 'Error'
|
|
40
26
|
|
|
41
27
|
FastifyError.prototype.toString = function () {
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/error",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.1",
|
|
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
|
"types": "types/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"lint": "standard",
|
|
9
|
+
"lint:fix": "standard --fix",
|
|
9
10
|
"test": "npm run test:unit && npm run test:typescript",
|
|
10
11
|
"test:unit": "tap",
|
|
11
12
|
"test:typescript": "tsd"
|
|
@@ -27,9 +28,10 @@
|
|
|
27
28
|
},
|
|
28
29
|
"homepage": "https://github.com/fastify/fastify-error#readme",
|
|
29
30
|
"devDependencies": {
|
|
31
|
+
"benchmark": "^2.1.4",
|
|
30
32
|
"standard": "^17.0.0",
|
|
31
33
|
"tap": "^16.0.0",
|
|
32
|
-
"tsd": "^0.
|
|
34
|
+
"tsd": "^0.28.0"
|
|
33
35
|
},
|
|
34
36
|
"tsd": {
|
|
35
37
|
"compilerOptions": {
|
package/test/index.test.js
CHANGED
|
File without changes
|
package/types/index.d.ts
CHANGED
|
@@ -1,19 +1,40 @@
|
|
|
1
|
-
declare function createError<C extends string, SC extends number
|
|
2
|
-
|
|
1
|
+
declare function createError<C extends string, SC extends number, Arg extends unknown[] = [any?, any?, any?]> (
|
|
2
|
+
code: C,
|
|
3
|
+
message: string,
|
|
4
|
+
statusCode: SC,
|
|
5
|
+
Base?: Error
|
|
6
|
+
): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>
|
|
3
7
|
|
|
4
|
-
|
|
8
|
+
declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
|
|
9
|
+
code: C,
|
|
10
|
+
message: string,
|
|
11
|
+
statusCode?: number,
|
|
12
|
+
Base?: Error
|
|
13
|
+
): createError.FastifyErrorConstructor<{ code: C }, Arg>
|
|
14
|
+
|
|
15
|
+
declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
|
|
16
|
+
code: string,
|
|
17
|
+
message: string,
|
|
18
|
+
statusCode?: number,
|
|
19
|
+
Base?: Error
|
|
20
|
+
): createError.FastifyErrorConstructor<{ code: string }, Arg>
|
|
21
|
+
|
|
22
|
+
type CreateError = typeof createError
|
|
5
23
|
|
|
6
24
|
declare namespace createError {
|
|
7
25
|
export interface FastifyError extends Error {
|
|
8
|
-
code: string
|
|
9
|
-
name: string
|
|
10
|
-
statusCode?: number
|
|
26
|
+
code: string
|
|
27
|
+
name: string
|
|
28
|
+
statusCode?: number
|
|
11
29
|
}
|
|
12
30
|
|
|
13
|
-
export interface FastifyErrorConstructor<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
31
|
+
export interface FastifyErrorConstructor<
|
|
32
|
+
E extends { code: string, statusCode?: number } = { code: string, statusCode?: number },
|
|
33
|
+
T extends unknown[] = [any?, any?, any?]
|
|
34
|
+
> {
|
|
35
|
+
new(...arg: T): FastifyError & E
|
|
36
|
+
(...arg: T): FastifyError & E
|
|
37
|
+
readonly prototype: FastifyError & E
|
|
17
38
|
}
|
|
18
39
|
|
|
19
40
|
export const createError: CreateError
|
package/types/index.test-d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import createError, { FastifyError, FastifyErrorConstructor } from '..'
|
|
2
|
-
import { expectType } from 'tsd'
|
|
2
|
+
import { expectType, expectError } from 'tsd'
|
|
3
3
|
|
|
4
4
|
const CustomError = createError('ERROR_CODE', 'message')
|
|
5
5
|
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
|
|
@@ -7,12 +7,61 @@ const err = new CustomError()
|
|
|
7
7
|
expectType<FastifyError & { code: 'ERROR_CODE' }>(err)
|
|
8
8
|
expectType<'ERROR_CODE'>(err.code)
|
|
9
9
|
expectType<string>(err.message)
|
|
10
|
-
expectType<number>(err.statusCode
|
|
10
|
+
expectType<number | undefined>(err.statusCode)
|
|
11
11
|
|
|
12
12
|
const CustomTypedError = createError('OTHER_CODE', 'message', 400)
|
|
13
|
-
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE'
|
|
13
|
+
expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
|
|
14
14
|
const typed = new CustomTypedError()
|
|
15
|
-
expectType<FastifyError & { code: 'OTHER_CODE'
|
|
15
|
+
expectType<FastifyError & { code: 'OTHER_CODE', statusCode: 400 }>(typed)
|
|
16
16
|
expectType<'OTHER_CODE'>(typed.code)
|
|
17
17
|
expectType<string>(typed.message)
|
|
18
18
|
expectType<400>(typed.statusCode)
|
|
19
|
+
|
|
20
|
+
/* eslint-disable no-new */
|
|
21
|
+
const CustomTypedArgError = createError<[string]>('OTHER_CODE', 'expect %s message', 400)
|
|
22
|
+
CustomTypedArgError('a')
|
|
23
|
+
expectError(CustomTypedArgError('a', 'b'))
|
|
24
|
+
expectError(new CustomTypedArgError('a', 'b'))
|
|
25
|
+
expectError(CustomTypedArgError(1))
|
|
26
|
+
expectError(new CustomTypedArgError(1))
|
|
27
|
+
|
|
28
|
+
const CustomTypedArgError2 = createError<string, number, [string]>('OTHER_CODE', 'expect %s message', 400)
|
|
29
|
+
CustomTypedArgError2('a')
|
|
30
|
+
expectError(CustomTypedArgError2('a', 'b'))
|
|
31
|
+
expectError(new CustomTypedArgError2('a', 'b'))
|
|
32
|
+
expectError(CustomTypedArgError2(1))
|
|
33
|
+
expectError(new CustomTypedArgError2(1))
|
|
34
|
+
|
|
35
|
+
const CustomTypedArgError3 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
|
|
36
|
+
expectError(CustomTypedArgError3('a'))
|
|
37
|
+
CustomTypedArgError3('a', 'b')
|
|
38
|
+
new CustomTypedArgError3('a', 'b')
|
|
39
|
+
expectError(CustomTypedArgError3(1))
|
|
40
|
+
expectError(new CustomTypedArgError3(1))
|
|
41
|
+
expectError(new CustomTypedArgError3(1, 2))
|
|
42
|
+
expectError(new CustomTypedArgError3('1', 2))
|
|
43
|
+
expectError(new CustomTypedArgError3(1, '2'))
|
|
44
|
+
|
|
45
|
+
const CustomTypedArgError4 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
|
|
46
|
+
expectError(CustomTypedArgError4('a'))
|
|
47
|
+
CustomTypedArgError4('a', 'b')
|
|
48
|
+
new CustomTypedArgError4('a', 'b')
|
|
49
|
+
expectError(CustomTypedArgError4(1))
|
|
50
|
+
expectError(new CustomTypedArgError4(1))
|
|
51
|
+
expectError(new CustomTypedArgError4(1, 2))
|
|
52
|
+
expectError(new CustomTypedArgError4('1', 2))
|
|
53
|
+
expectError(new CustomTypedArgError4(1, '2'))
|
|
54
|
+
|
|
55
|
+
const CustomTypedArgError5 = createError<[string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400)
|
|
56
|
+
expectError(CustomTypedArgError5('a'))
|
|
57
|
+
expectError(new CustomTypedArgError5('a', 'b'))
|
|
58
|
+
expectError(new CustomTypedArgError5('a', 'b', 'c'))
|
|
59
|
+
CustomTypedArgError5('a', 'b', 'c', 'd')
|
|
60
|
+
expectError(new CustomTypedArgError5('a', 'b', 'c', 'd', 'e'))
|
|
61
|
+
|
|
62
|
+
const CustomTypedArgError6 = createError<string, number, [string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400)
|
|
63
|
+
expectError(CustomTypedArgError6('a'))
|
|
64
|
+
expectError(new CustomTypedArgError6('a', 'b'))
|
|
65
|
+
expectError(new CustomTypedArgError6('a', 'b', 'c'))
|
|
66
|
+
CustomTypedArgError6('a', 'b', 'c', 'd')
|
|
67
|
+
expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))
|