@fastify/error 3.2.0 → 3.3.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.
@@ -2,6 +2,11 @@ name: CI
2
2
 
3
3
  on:
4
4
  push:
5
+ branches:
6
+ - main
7
+ - master
8
+ - next
9
+ - 'v*'
5
10
  paths-ignore:
6
11
  - 'docs/**'
7
12
  - '*.md'
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
@@ -1,48 +1,43 @@
1
1
  'use strict'
2
2
 
3
- const { inherits, format } = require('util')
3
+ const { format } = require('util')
4
+
5
+ function toString () {
6
+ return `${this.name} [${this.code}]: ${this.message}`
7
+ }
4
8
 
5
9
  function createError (code, message, statusCode = 500, Base = Error) {
6
10
  if (!code) throw new Error('Fastify error code must not be empty')
7
11
  if (!message) throw new Error('Fastify error message must not be empty')
8
12
 
9
13
  code = code.toUpperCase()
14
+ !statusCode && (statusCode = undefined)
10
15
 
11
- function FastifyError (a, b, c) {
16
+ function FastifyError (...args) {
12
17
  if (!new.target) {
13
- return new FastifyError(...arguments)
18
+ return new FastifyError(...args)
14
19
  }
15
- Error.captureStackTrace(this, FastifyError)
16
- this.name = 'FastifyError'
17
20
  this.code = code
21
+ this.name = 'FastifyError'
22
+ this.statusCode = statusCode
18
23
 
19
- // more performant than spread (...) operator
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
- }
24
+ this.message = format(message, ...args)
36
25
 
37
- this.statusCode = statusCode || undefined
26
+ Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)
38
27
  }
39
- FastifyError.prototype[Symbol.toStringTag] = 'Error'
40
28
 
41
- FastifyError.prototype.toString = function () {
42
- return `${this.name} [${this.code}]: ${this.message}`
43
- }
29
+ FastifyError.prototype = Object.create(Base.prototype, {
30
+ constructor: {
31
+ value: FastifyError,
32
+ enumerable: false,
33
+ writable: true,
34
+ configurable: true
35
+ }
36
+ })
37
+
38
+ FastifyError.prototype[Symbol.toStringTag] = 'Error'
44
39
 
45
- inherits(FastifyError, Base)
40
+ FastifyError.prototype.toString = toString
46
41
 
47
42
  return FastifyError
48
43
  }
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@fastify/error",
3
- "version": "3.2.0",
3
+ "version": "3.3.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
  "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.25.0"
34
+ "tsd": "^0.28.0"
33
35
  },
34
36
  "tsd": {
35
37
  "compilerOptions": {
package/types/index.d.ts CHANGED
@@ -1,19 +1,40 @@
1
- declare function createError<C extends string, SC extends number>(code: C, message: string, statusCode: SC, Base?: Error): createError.FastifyErrorConstructor<{ code: C; statusCode: SC }>;
2
- declare function createError<C extends string>(code: C, message: string, statusCode?: number, Base?: Error): createError.FastifyErrorConstructor<{ code: C; }>;
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
- type CreateError = typeof createError;
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<E extends { code: string; statusCode?: number } = { code: string; statusCode?: number }> {
14
- new (a?: any, b?: any, c?: any): FastifyError & E;
15
- (a?: any, b?: any, c?: any): FastifyError & E;
16
- readonly prototype: FastifyError & E;
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
@@ -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'; statusCode: 400 }>>(CustomTypedError)
13
+ expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
14
14
  const typed = new CustomTypedError()
15
- expectType<FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }>(typed)
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'))