@fastify/error 3.3.0 → 3.4.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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Set default behavior to automatically convert line endings
2
+ * text=auto eol=lf
package/README.md CHANGED
@@ -22,12 +22,12 @@ createError(code, message [, statusCode [, Base]])
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
- - `Base` (`Error`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)
25
+ - `Base` (`ErrorConstructor`, optional) - The base error object that will be used. (eg `TypeError`, `RangeError`)
26
26
 
27
27
  ```js
28
28
  const createError = require('@fastify/error')
29
- const CustomError = createError('ERROR_CODE', 'message')
30
- console.log(new CustomError())
29
+ const CustomError = createError('ERROR_CODE', 'Hello')
30
+ console.log(new CustomError()) // error.message => 'Hello'
31
31
  ```
32
32
 
33
33
  How to use an interpolated string:
@@ -37,6 +37,15 @@ const CustomError = createError('ERROR_CODE', 'Hello %s')
37
37
  console.log(new CustomError('world')) // error.message => 'Hello world'
38
38
  ```
39
39
 
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
+
40
49
  ### TypeScript
41
50
 
42
51
  It is possible to limit your error constructor with a generic type using TypeScript:
@@ -46,8 +55,6 @@ const CustomError = createError<[string]>('ERROR_CODE', 'Hello %s')
46
55
  new CustomError('world')
47
56
  //@ts-expect-error
48
57
  new CustomError(1)
49
- //@ts-expect-error
50
- new CustomError(1)
51
58
  ```
52
59
 
53
60
  ## License
@@ -7,10 +7,12 @@ const FastifyError = createError('CODE', 'Not available')
7
7
  const FastifyError1 = createError('CODE', 'Not %s available')
8
8
  const FastifyError2 = createError('CODE', 'Not %s available %s')
9
9
 
10
+ const cause = new Error('cause')
10
11
  new benchmark.Suite()
11
12
  .add('instantiate Error', function () { new Error() }, { minSamples: 100 }) // eslint-disable-line no-new
12
13
  .add('instantiate FastifyError', function () { new FastifyError() }, { minSamples: 100 }) // eslint-disable-line no-new
13
14
  .add('instantiate FastifyError arg 1', function () { new FastifyError1('q') }, { minSamples: 100 }) // eslint-disable-line no-new
14
15
  .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
15
17
  .on('cycle', function onCycle (event) { console.log(String(event.target)) })
16
18
  .run({ async: false })
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { format } = require('util')
3
+ const { format } = require('node:util')
4
4
 
5
5
  function toString () {
6
6
  return `${this.name} [${this.code}]: ${this.message}`
@@ -17,10 +17,16 @@ function createError (code, message, statusCode = 500, Base = Error) {
17
17
  if (!new.target) {
18
18
  return new FastifyError(...args)
19
19
  }
20
+
20
21
  this.code = code
21
22
  this.name = 'FastifyError'
22
23
  this.statusCode = statusCode
23
24
 
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
+
24
30
  this.message = format(message, ...args)
25
31
 
26
32
  Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@fastify/error",
3
- "version": "3.3.0",
3
+ "version": "3.4.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
+ "type": "commonjs",
6
7
  "types": "types/index.d.ts",
7
8
  "scripts": {
8
9
  "lint": "standard",
@@ -31,7 +32,7 @@
31
32
  "benchmark": "^2.1.4",
32
33
  "standard": "^17.0.0",
33
34
  "tap": "^16.0.0",
34
- "tsd": "^0.28.0"
35
+ "tsd": "^0.29.0"
35
36
  },
36
37
  "tsd": {
37
38
  "compilerOptions": {
@@ -1,8 +1,6 @@
1
1
  'use strict'
2
2
 
3
- /* eslint no-prototype-builtins: 0 */
4
-
5
- const test = require('tap').test
3
+ const { test } = require('tap')
6
4
  const createError = require('..')
7
5
 
8
6
  test('Create error with zero parameter', t => {
@@ -154,3 +152,33 @@ test('Create the error without the new keyword', t => {
154
152
  t.equal(err.statusCode, 500)
155
153
  t.ok(err.stack)
156
154
  })
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 cause and message', t => {
167
+ t.plan(2)
168
+ const cause = new Error('HEY')
169
+ const NewError = createError('CODE', 'Not available: %s')
170
+ const err = NewError('foo', { cause })
171
+
172
+ t.ok(err instanceof Error)
173
+ t.equal(err.cause, cause)
174
+ })
175
+
176
+ test('Create an error with last argument null', t => {
177
+ t.plan(2)
178
+ const cause = new Error('HEY')
179
+ const NewError = createError('CODE', 'Not available')
180
+ const err = NewError({ cause }, null)
181
+
182
+ t.ok(err instanceof Error)
183
+ t.notOk(err.cause)
184
+ })
package/types/index.d.ts CHANGED
@@ -2,21 +2,21 @@ 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?: Error
5
+ Base?: ErrorConstructor
6
6
  ): createError.FastifyErrorConstructor<{ code: C, statusCode: SC }, Arg>
7
7
 
8
8
  declare function createError<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
9
9
  code: C,
10
10
  message: string,
11
11
  statusCode?: number,
12
- Base?: Error
12
+ Base?: ErrorConstructor
13
13
  ): createError.FastifyErrorConstructor<{ code: C }, Arg>
14
14
 
15
15
  declare function createError<Arg extends unknown[] = [any?, any?, any?]> (
16
16
  code: string,
17
17
  message: string,
18
18
  statusCode?: number,
19
- Base?: Error
19
+ Base?: ErrorConstructor
20
20
  ): createError.FastifyErrorConstructor<{ code: string }, Arg>
21
21
 
22
22
  type CreateError = typeof createError
@@ -65,3 +65,8 @@ expectError(new CustomTypedArgError6('a', 'b'))
65
65
  expectError(new CustomTypedArgError6('a', 'b', 'c'))
66
66
  CustomTypedArgError6('a', 'b', 'c', 'd')
67
67
  expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))
68
+
69
+
70
+ const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError)
71
+ expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor)
72
+ CustomErrorWithErrorConstructor({cause: new Error('Error')})