@fastify/error 4.2.0 → 4.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,12 +2,52 @@ version: 2
2
2
  updates:
3
3
  - package-ecosystem: "github-actions"
4
4
  directory: "/"
5
+ commit-message:
6
+ # Prefix all commit messages with "chore: "
7
+ prefix: "chore"
5
8
  schedule:
6
- interval: "monthly"
9
+ interval: "weekly"
10
+ cooldown:
11
+ default-days: 7
12
+ allow:
13
+ - dependency-name: "*"
14
+ update-types:
15
+ - "version-update:semver-major"
7
16
  open-pull-requests-limit: 10
8
17
 
9
18
  - package-ecosystem: "npm"
10
19
  directory: "/"
20
+ commit-message:
21
+ # Prefix all commit messages with "chore: "
22
+ prefix: "chore"
11
23
  schedule:
12
- interval: "monthly"
24
+ interval: "weekly"
25
+ cooldown:
26
+ default-days: 7
27
+ versioning-strategy: "increase-if-necessary"
28
+ allow:
29
+ - dependency-name: "*"
30
+ update-types:
31
+ - "version-update:semver-major"
32
+ ignore:
33
+ # TODO: remove ignore until neostandard support ESLint 10
34
+ - dependency-name: "eslint"
35
+ - dependency-name: "neostandard"
36
+ - dependency-name: "@stylistic/*"
13
37
  open-pull-requests-limit: 10
38
+ groups:
39
+ # Production dependencies with breaking changes
40
+ dependencies:
41
+ dependency-type: "production"
42
+ # ESLint related dependencies
43
+ dev-dependencies-eslint:
44
+ patterns:
45
+ - "eslint"
46
+ - "neostandard"
47
+ - "@stylistic/*"
48
+ # TypeScript related dependencies
49
+ dev-dependencies-typescript:
50
+ patterns:
51
+ - "@types/*"
52
+ - "tstyche"
53
+ - "typescript"
@@ -14,6 +14,11 @@ on:
14
14
  - 'docs/**'
15
15
  - '*.md'
16
16
 
17
+ # This allows a subsequently queued workflow run to interrupt previous runs
18
+ concurrency:
19
+ group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
20
+ cancel-in-progress: true
21
+
17
22
  permissions:
18
23
  contents: read
19
24
 
@@ -22,7 +27,7 @@ jobs:
22
27
  permissions:
23
28
  contents: write
24
29
  pull-requests: write
25
- uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
30
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
26
31
  with:
27
32
  license-check: true
28
33
  lint: true
@@ -0,0 +1,19 @@
1
+ name: Lock Threads
2
+
3
+ on:
4
+ schedule:
5
+ - cron: '0 0 1 * *'
6
+ workflow_dispatch:
7
+
8
+ concurrency:
9
+ group: lock
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ lock-threads:
16
+ permissions:
17
+ issues: write
18
+ pull-requests: write
19
+ uses: fastify/workflows/.github/workflows/lock-threads.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
@@ -0,0 +1,24 @@
1
+ name: Release the package
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ semver:
7
+ description: 'Release bump type'
8
+ required: true
9
+ type: choice
10
+ options:
11
+ - patch
12
+ - minor
13
+ - major
14
+
15
+ permissions: {}
16
+
17
+ jobs:
18
+ release:
19
+ permissions:
20
+ id-token: write # required for npm provenance via OIDC
21
+ contents: write # required to push the release commit and the tag
22
+ uses: fastify/workflows/.github/workflows/reusable-release.yml@731d0cca9a6a5b9586dad6b0326d49001ab9216f
23
+ with:
24
+ semver: ${{ inputs.semver }}
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2020 Fastify
3
+ Copyright (c) 2020-present The Fastify team <https://github.com/fastify/fastify#team>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -47,6 +47,45 @@ console.log(new CustomError('world', {cause: new Error('cause')}))
47
47
  // error.cause => Error('cause')
48
48
  ```
49
49
 
50
+ #### RFC 9457 problem details
51
+
52
+ The module also exports `createRFC9457Error`, which creates error objects that can be serialized as [RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) problem details objects (RFC 9457 obsoletes RFC 7807):
53
+
54
+ ```js
55
+ createRFC9457Error(code, message [, statusCode [, opts [, Base [, captureStackTrace]]]])
56
+ ```
57
+
58
+ It accepts the same parameters as `createError`, plus an optional `opts` object:
59
+
60
+ - `type` (`string`, optional) - A URI reference that identifies the problem type. Defaults to `'about:blank'`.
61
+ - `title` (`string`, optional) - A short, human-readable summary of the problem type. Defaults to the HTTP status reason phrase for the error's status code (e.g. `'Not Found'` for `404`).
62
+
63
+ The created error instances expose a `toProblemDetails(instance [, details])` method that returns the problem details object:
64
+
65
+ ```js
66
+ const { createRFC9457Error } = require('@fastify/error')
67
+
68
+ const QuotaExceeded = createRFC9457Error(
69
+ 'QUOTA_EXCEEDED',
70
+ 'Quota exceeded by %d requests',
71
+ 429,
72
+ { type: 'https://example.net/probs/quota', title: 'Quota Exceeded' }
73
+ )
74
+
75
+ console.log(new QuotaExceeded(7).toProblemDetails('/api/limits', { retryAfter: 60 }))
76
+ // {
77
+ // type: 'https://example.net/probs/quota',
78
+ // title: 'Quota Exceeded',
79
+ // status: 429,
80
+ // detail: 'Quota exceeded by 7 requests',
81
+ // instance: '/api/limits',
82
+ // code: 'QUOTA_EXCEEDED',
83
+ // details: { retryAfter: 60 }
84
+ // }
85
+ ```
86
+
87
+ `instance` is a URI reference that identifies the specific occurrence of the problem (e.g. the request path), and `details` carries additional extension members. `code` and `details` are [extension members](https://www.rfc-editor.org/rfc/rfc9457.html#name-extension-members) and are not defined by RFC 9457 itself.
88
+
50
89
  ### TypeScript
51
90
 
52
91
  It is possible to limit your error constructor with a generic type using TypeScript:
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
 
3
+ const { STATUS_CODES } = require('node:http')
3
4
  const { format } = require('node:util')
4
5
 
5
6
  function toString () {
@@ -90,6 +91,38 @@ function createError (code, message, statusCode = 500, Base = Error, captureStac
90
91
  return FastifyError
91
92
  }
92
93
 
94
+ function createRFC9457Error (
95
+ code,
96
+ message,
97
+ statusCode = 500,
98
+ opts = {},
99
+ Base = Error,
100
+ captureStackTrace = createError.captureStackTrace
101
+ ) {
102
+ const RFC9457Error = createError(code, message, statusCode, Base, captureStackTrace)
103
+
104
+ const type = opts.type || 'about:blank'
105
+ const title = opts.title
106
+
107
+ RFC9457Error.prototype.toProblemDetails = function (instance = '', details = {}) {
108
+ const status = this.statusCode ?? statusCode
109
+
110
+ return {
111
+ type,
112
+ // RFC 9457 recommends the HTTP status reason phrase as the title
113
+ // when no dedicated title is available
114
+ title: title || STATUS_CODES[status] || 'Unknown Error',
115
+ status,
116
+ detail: this.message,
117
+ instance,
118
+ code: this.code,
119
+ details
120
+ }
121
+ }
122
+
123
+ return RFC9457Error
124
+ }
125
+
93
126
  createError.captureStackTrace = true
94
127
 
95
128
  const FastifyErrorConstructor = createError(FastifyGenericErrorSymbol, 'Fastify Error', 500, Error)
@@ -98,3 +131,4 @@ module.exports = createError
98
131
  module.exports.FastifyError = FastifyErrorConstructor
99
132
  module.exports.default = createError
100
133
  module.exports.createError = createError
134
+ module.exports.createRFC9457Error = createRFC9457Error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/error",
3
- "version": "4.2.0",
3
+ "version": "4.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
  "type": "commonjs",
@@ -10,7 +10,7 @@
10
10
  "lint:fix": "eslint --fix",
11
11
  "test": "npm run test:unit && npm run test:typescript",
12
12
  "test:unit": "c8 --100 node --test",
13
- "test:typescript": "tsd"
13
+ "test:typescript": "tstyche"
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
@@ -59,15 +59,10 @@
59
59
  ],
60
60
  "devDependencies": {
61
61
  "benchmark": "^2.1.4",
62
- "c8": "^10.1.2",
62
+ "c8": "^12.0.0",
63
63
  "eslint": "^9.17.0",
64
- "neostandard": "^0.12.0",
65
- "tsd": "^0.32.0"
66
- },
67
- "tsd": {
68
- "compilerOptions": {
69
- "esModuleInterop": true
70
- }
64
+ "neostandard": "^0.13.0",
65
+ "tstyche": "^7.1.0"
71
66
  },
72
67
  "publishConfig": {
73
68
  "access": "public"
@@ -147,7 +147,7 @@ test('ensure that instanceof works accross different installations of the fastif
147
147
  console.error('expectedPathOfFastifyError', expectedPathOfFastifyError)
148
148
  throw new Error('fastify-error should be required from the node_modules directory of the test-project')
149
149
  }
150
-
150
+
151
151
  const Boom = createError('Boom', 'Boom', 500)
152
152
  const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
153
153
  const NotChildBoom = createError('NotChildBoom', 'NotChildBoom', 500, Boom)
@@ -191,7 +191,7 @@ test('ensure that instanceof works accross different installations of the fastif
191
191
  console.error('expectedPathOfFastifyError', expectedPathOfFastifyError)
192
192
  throw new Error('fastify-error should be required from the node_modules directory of dep')
193
193
  }
194
-
194
+
195
195
  const Boom = createError('Boom', 'Boom', 500)
196
196
  const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
197
197
 
@@ -0,0 +1,109 @@
1
+ 'use strict'
2
+
3
+ const test = require('node:test')
4
+ const { createRFC9457Error } = require('..')
5
+
6
+ test('toProblemDetails() with defaults', (t) => {
7
+ t.plan(1)
8
+
9
+ const E = createRFC9457Error('CODE', 'oops')
10
+ const e = new E()
11
+ const pd = e.toProblemDetails()
12
+
13
+ t.assert.deepStrictEqual(pd, {
14
+ type: 'about:blank',
15
+ title: 'Internal Server Error',
16
+ status: 500,
17
+ detail: 'oops',
18
+ instance: '',
19
+ code: 'CODE',
20
+ details: {}
21
+ })
22
+ })
23
+
24
+ test('title defaults to the HTTP reason phrase for the status', (t) => {
25
+ t.plan(1)
26
+
27
+ const NotFound = createRFC9457Error('NOT_FOUND', 'user %s does not exist', 404)
28
+ const pd = new NotFound('alice').toProblemDetails('/users/alice')
29
+
30
+ t.assert.deepStrictEqual(pd, {
31
+ type: 'about:blank',
32
+ title: 'Not Found',
33
+ status: 404,
34
+ detail: 'user alice does not exist',
35
+ instance: '/users/alice',
36
+ code: 'NOT_FOUND',
37
+ details: {}
38
+ })
39
+ })
40
+
41
+ test('toProblemDetails() with custom type, title and extension details', (t) => {
42
+ t.plan(1)
43
+
44
+ const Warning = createRFC9457Error(
45
+ 'WARN',
46
+ 'Threshold exceeded by %d',
47
+ 429,
48
+ { type: 'https://example.net/probs/warn', title: 'Warning' }
49
+ )
50
+ const w = new Warning(7)
51
+
52
+ const pd = w.toProblemDetails('/limit', { retryAfter: 60, exceeded: true })
53
+
54
+ t.assert.deepStrictEqual(pd, {
55
+ type: 'https://example.net/probs/warn',
56
+ title: 'Warning',
57
+ status: 429,
58
+ detail: 'Threshold exceeded by 7',
59
+ instance: '/limit',
60
+ code: 'WARN',
61
+ details: { retryAfter: 60, exceeded: true }
62
+ })
63
+ })
64
+
65
+ test('title falls back to a generic label for unknown status codes', (t) => {
66
+ t.plan(2)
67
+
68
+ const E = createRFC9457Error('WEIRD', 'unusual status', 599)
69
+ const pd = new E().toProblemDetails()
70
+
71
+ t.assert.equal(pd.title, 'Unknown Error')
72
+ t.assert.equal(pd.status, 599)
73
+ })
74
+
75
+ test('toProblemDetails() falls back to the factory statusCode when the instance has none', (t) => {
76
+ t.plan(2)
77
+
78
+ // createError drops a falsy statusCode from the instance
79
+ const E = createRFC9457Error('CODE', 'oops', 0)
80
+ const pd = new E().toProblemDetails()
81
+
82
+ t.assert.equal(pd.status, 0)
83
+ t.assert.equal(pd.title, 'Unknown Error')
84
+ })
85
+
86
+ test('toProblemDetails() honors an overridden instance statusCode', (t) => {
87
+ t.plan(2)
88
+
89
+ const E = createRFC9457Error('CODE', 'oops', 500)
90
+ const e = new E()
91
+ e.statusCode = 502
92
+ const pd = e.toProblemDetails()
93
+
94
+ t.assert.equal(pd.status, 502)
95
+ t.assert.equal(pd.title, 'Bad Gateway')
96
+ })
97
+
98
+ test('created errors behave like regular fastify errors', (t) => {
99
+ t.plan(5)
100
+
101
+ const E = createRFC9457Error('CODE', 'hey %s', 400)
102
+ const err = new E('alice')
103
+
104
+ t.assert.ok(err instanceof Error)
105
+ t.assert.equal(err.name, 'FastifyError')
106
+ t.assert.equal(err.code, 'CODE')
107
+ t.assert.equal(err.message, 'hey alice')
108
+ t.assert.equal(err.toString(), 'FastifyError [CODE]: hey alice')
109
+ })
package/types/index.d.ts CHANGED
@@ -40,8 +40,63 @@ declare namespace createError {
40
40
  readonly prototype: FastifyError & E
41
41
  }
42
42
 
43
+ export interface RFC9457ErrorOptions {
44
+ type?: string
45
+ title?: string
46
+ }
47
+
48
+ export interface RFC9457ProblemDetails {
49
+ type: string
50
+ title: string
51
+ status: number
52
+ detail: string
53
+ instance: string
54
+ code: string
55
+ details: Record<string, unknown>
56
+ }
57
+
58
+ export interface RFC9457Error extends FastifyError {
59
+ toProblemDetails(instance?: string, details?: Record<string, unknown>): RFC9457ProblemDetails
60
+ }
61
+
62
+ export interface RFC9457ErrorConstructor<
63
+ E extends { code: string, statusCode?: number } = { code: string, statusCode?: number },
64
+ T extends unknown[] = [any?, any?, any?]
65
+ > {
66
+ new(...arg: T): RFC9457Error & E
67
+ (...arg: T): RFC9457Error & E
68
+ readonly prototype: RFC9457Error & E
69
+ }
70
+
43
71
  export const FastifyError: FastifyErrorConstructor
44
72
 
73
+ export function createRFC9457Error<C extends string, SC extends number, Arg extends unknown[] = [any?, any?, any?]> (
74
+ code: C,
75
+ message: string,
76
+ statusCode: SC,
77
+ opts?: RFC9457ErrorOptions,
78
+ Base?: ErrorConstructor,
79
+ captureStackTrace?: boolean
80
+ ): RFC9457ErrorConstructor<{ code: C, statusCode: SC }, Arg>
81
+
82
+ export function createRFC9457Error<C extends string, Arg extends unknown[] = [any?, any?, any?]> (
83
+ code: C,
84
+ message: string,
85
+ statusCode?: number,
86
+ opts?: RFC9457ErrorOptions,
87
+ Base?: ErrorConstructor,
88
+ captureStackTrace?: boolean
89
+ ): RFC9457ErrorConstructor<{ code: C }, Arg>
90
+
91
+ export function createRFC9457Error<Arg extends unknown[] = [any?, any?, any?]> (
92
+ code: string,
93
+ message: string,
94
+ statusCode?: number,
95
+ opts?: RFC9457ErrorOptions,
96
+ Base?: ErrorConstructor,
97
+ captureStackTrace?: boolean
98
+ ): RFC9457ErrorConstructor<{ code: string }, Arg>
99
+
45
100
  export const createError: CreateError
46
101
  export { createError as default }
47
102
  }
@@ -0,0 +1,199 @@
1
+ import { expect } from 'tstyche'
2
+ import createError, { FastifyError, FastifyErrorConstructor, RFC9457Error, RFC9457ErrorConstructor, RFC9457ProblemDetails, createRFC9457Error } from '..'
3
+
4
+ const CustomError = createError('ERROR_CODE', 'message')
5
+ expect(CustomError).type.toBe<
6
+ FastifyErrorConstructor<{ code: 'ERROR_CODE' }>
7
+ >()
8
+ const err = new CustomError()
9
+ expect(err).type.toBe<FastifyError & { code: 'ERROR_CODE' }>()
10
+ expect(err.code).type.toBe<'ERROR_CODE'>()
11
+ expect(err.message).type.toBe<string>()
12
+ expect(err.statusCode).type.toBe<number | undefined>()
13
+
14
+ const CustomErrorNoStackTrace = createError(
15
+ 'ERROR_CODE',
16
+ 'message',
17
+ undefined,
18
+ undefined,
19
+ false
20
+ )
21
+ expect(CustomErrorNoStackTrace).type.toBe<
22
+ FastifyErrorConstructor<{ code: 'ERROR_CODE' }>
23
+ >()
24
+ const errNoStackTrace = new CustomErrorNoStackTrace()
25
+ expect(errNoStackTrace).type.toBe<FastifyError & { code: 'ERROR_CODE' }>()
26
+ expect(errNoStackTrace.code).type.toBe<'ERROR_CODE'>()
27
+ expect(errNoStackTrace.message).type.toBe<string>()
28
+ expect(errNoStackTrace.statusCode).type.toBe<number | undefined>()
29
+
30
+ const CustomTypedError = createError('OTHER_CODE', 'message', 400)
31
+ expect(CustomTypedError).type.toBe<
32
+ FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>
33
+ >()
34
+ const typed = new CustomTypedError()
35
+ expect(typed).type.toBe<
36
+ FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }
37
+ >()
38
+ expect(typed.code).type.toBe<'OTHER_CODE'>()
39
+ expect(typed.message).type.toBe<string>()
40
+ expect(typed.statusCode).type.toBe<400>()
41
+
42
+ /* eslint-disable no-new */
43
+ const CustomTypedArgError = createError<[string]>(
44
+ 'OTHER_CODE',
45
+ 'expect %s message',
46
+ 400
47
+ )
48
+ CustomTypedArgError('a')
49
+ expect(CustomTypedArgError).type.not.toBeCallableWith('a', 'b')
50
+ expect(CustomTypedArgError).type.not.toBeConstructableWith('a', 'b')
51
+ expect(CustomTypedArgError).type.not.toBeCallableWith(1)
52
+ expect(CustomTypedArgError).type.not.toBeConstructableWith(1)
53
+
54
+ const CustomTypedArgError2 = createError<string, number, [string]>(
55
+ 'OTHER_CODE',
56
+ 'expect %s message',
57
+ 400
58
+ )
59
+ CustomTypedArgError2('a')
60
+ expect(CustomTypedArgError2).type.not.toBeCallableWith('a', 'b')
61
+ expect(CustomTypedArgError2).type.not.toBeConstructableWith('a', 'b')
62
+ expect(CustomTypedArgError2).type.not.toBeCallableWith(1)
63
+ expect(CustomTypedArgError2).type.not.toBeConstructableWith(1)
64
+
65
+ const CustomTypedArgError3 = createError<string, number, [string, string]>(
66
+ 'OTHER_CODE',
67
+ 'expect %s message but got %s',
68
+ 400
69
+ )
70
+ expect(CustomTypedArgError3).type.not.toBeCallableWith('a')
71
+ CustomTypedArgError3('a', 'b')
72
+ new CustomTypedArgError3('a', 'b')
73
+ expect(CustomTypedArgError3).type.not.toBeCallableWith(1)
74
+ expect(CustomTypedArgError3).type.not.toBeConstructableWith(1)
75
+ expect(CustomTypedArgError3).type.not.toBeConstructableWith(1, 2)
76
+ expect(CustomTypedArgError3).type.not.toBeConstructableWith('1', 2)
77
+ expect(CustomTypedArgError3).type.not.toBeConstructableWith(1, '2')
78
+
79
+ const CustomTypedArgError4 = createError<string, number, [string, string]>(
80
+ 'OTHER_CODE',
81
+ 'expect %s message but got %s',
82
+ 400
83
+ )
84
+ expect(CustomTypedArgError4).type.not.toBeCallableWith('a')
85
+ CustomTypedArgError4('a', 'b')
86
+ new CustomTypedArgError4('a', 'b')
87
+ expect(CustomTypedArgError4).type.not.toBeCallableWith(1)
88
+ expect(CustomTypedArgError4).type.not.toBeConstructableWith(1)
89
+ expect(CustomTypedArgError4).type.not.toBeConstructableWith(1, 2)
90
+ expect(CustomTypedArgError4).type.not.toBeConstructableWith('1', 2)
91
+ expect(CustomTypedArgError4).type.not.toBeConstructableWith(1, '2')
92
+
93
+ const CustomTypedArgError5 = createError<[string, string, string, string]>(
94
+ 'OTHER_CODE',
95
+ 'expect %s message but got %s. Please contact %s by emailing to %s',
96
+ 400
97
+ )
98
+ expect(CustomTypedArgError5).type.not.toBeCallableWith('a')
99
+ expect(CustomTypedArgError5).type.not.toBeConstructableWith('a', 'b')
100
+ expect(CustomTypedArgError5).type.not.toBeConstructableWith('a', 'b', 'c')
101
+ CustomTypedArgError5('a', 'b', 'c', 'd')
102
+ expect(CustomTypedArgError5).type.not.toBeConstructableWith(
103
+ 'a',
104
+ 'b',
105
+ 'c',
106
+ 'd',
107
+ 'e'
108
+ )
109
+
110
+ const CustomTypedArgError6 = createError<
111
+ string,
112
+ number,
113
+ [string, string, string, string]
114
+ >(
115
+ 'OTHER_CODE',
116
+ 'expect %s message but got %s. Please contact %s by emailing to %s',
117
+ 400
118
+ )
119
+ expect(CustomTypedArgError6).type.not.toBeCallableWith('a')
120
+ expect(CustomTypedArgError6).type.not.toBeConstructableWith('a', 'b')
121
+ expect(CustomTypedArgError6).type.not.toBeConstructableWith('a', 'b', 'c')
122
+ CustomTypedArgError6('a', 'b', 'c', 'd')
123
+ expect(CustomTypedArgError6).type.not.toBeConstructableWith(
124
+ 'a',
125
+ 'b',
126
+ 'c',
127
+ 'd',
128
+ 'e'
129
+ )
130
+
131
+ const CustomErrorWithErrorConstructor = createError(
132
+ 'ERROR_CODE',
133
+ 'message',
134
+ 500,
135
+ TypeError
136
+ )
137
+ expect(CustomErrorWithErrorConstructor).type.toBe<
138
+ FastifyErrorConstructor<{ code: 'ERROR_CODE'; statusCode: 500 }>
139
+ >()
140
+ CustomErrorWithErrorConstructor({ cause: new Error('Error') })
141
+ const customErrorWithErrorConstructor = CustomErrorWithErrorConstructor()
142
+ if (customErrorWithErrorConstructor instanceof FastifyError) {
143
+ expect(customErrorWithErrorConstructor.code).type.toBe<'ERROR_CODE'>()
144
+ expect(customErrorWithErrorConstructor.message).type.toBe<string>()
145
+ expect(customErrorWithErrorConstructor.statusCode).type.toBe<500>()
146
+ }
147
+
148
+ const error = new FastifyError('ERROR_CODE', 'message', 500)
149
+ if (error instanceof FastifyError) {
150
+ expect(error.code).type.toBe<string>()
151
+ expect(error.message).type.toBe<string>()
152
+ expect(error.statusCode).type.toBe<number | undefined>()
153
+ }
154
+
155
+ const RFC9457CustomError = createRFC9457Error('ERROR_CODE', 'message')
156
+ expect(RFC9457CustomError).type.toBe<
157
+ RFC9457ErrorConstructor<{ code: 'ERROR_CODE' }>
158
+ >()
159
+ const rfc9457Err = new RFC9457CustomError()
160
+ expect(rfc9457Err).type.toBe<RFC9457Error & { code: 'ERROR_CODE' }>()
161
+ expect(rfc9457Err.code).type.toBe<'ERROR_CODE'>()
162
+ expect(rfc9457Err.message).type.toBe<string>()
163
+ expect(rfc9457Err.statusCode).type.toBe<number | undefined>()
164
+
165
+ const problemDetails = rfc9457Err.toProblemDetails('/path', { extra: 1 })
166
+ expect(problemDetails).type.toBe<RFC9457ProblemDetails>()
167
+ expect(problemDetails.type).type.toBe<string>()
168
+ expect(problemDetails.title).type.toBe<string>()
169
+ expect(problemDetails.status).type.toBe<number>()
170
+ expect(problemDetails.detail).type.toBe<string>()
171
+ expect(problemDetails.instance).type.toBe<string>()
172
+ expect(problemDetails.code).type.toBe<string>()
173
+ expect(problemDetails.details).type.toBe<Record<string, unknown>>()
174
+
175
+ const RFC9457TypedError = createRFC9457Error(
176
+ 'OTHER_CODE',
177
+ 'message',
178
+ 400,
179
+ { type: 'https://example.net/probs/other', title: 'Other' }
180
+ )
181
+ expect(RFC9457TypedError).type.toBe<
182
+ RFC9457ErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>
183
+ >()
184
+ const rfc9457Typed = new RFC9457TypedError()
185
+ expect(rfc9457Typed).type.toBe<
186
+ RFC9457Error & { code: 'OTHER_CODE'; statusCode: 400 }
187
+ >()
188
+ expect(rfc9457Typed.statusCode).type.toBe<400>()
189
+
190
+ const RFC9457TypedArgError = createRFC9457Error<[string]>(
191
+ 'ARG_CODE',
192
+ 'expect %s message',
193
+ 400
194
+ )
195
+ RFC9457TypedArgError('a')
196
+ expect(RFC9457TypedArgError).type.not.toBeCallableWith('a', 'b')
197
+ expect(RFC9457TypedArgError).type.not.toBeConstructableWith('a', 'b')
198
+ expect(RFC9457TypedArgError).type.not.toBeCallableWith(1)
199
+ expect(RFC9457TypedArgError).type.not.toBeConstructableWith(1)
@@ -1,92 +0,0 @@
1
- import createError, { FastifyError, FastifyErrorConstructor } from '..'
2
- import { expectType, expectError } from 'tsd'
3
-
4
- const CustomError = createError('ERROR_CODE', 'message')
5
- expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE' }>>(CustomError)
6
- const err = new CustomError()
7
- expectType<FastifyError & { code: 'ERROR_CODE' }>(err)
8
- expectType<'ERROR_CODE'>(err.code)
9
- expectType<string>(err.message)
10
- expectType<number | undefined>(err.statusCode)
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
-
20
- const CustomTypedError = createError('OTHER_CODE', 'message', 400)
21
- expectType<FastifyErrorConstructor<{ code: 'OTHER_CODE', statusCode: 400 }>>(CustomTypedError)
22
- const typed = new CustomTypedError()
23
- expectType<FastifyError & { code: 'OTHER_CODE', statusCode: 400 }>(typed)
24
- expectType<'OTHER_CODE'>(typed.code)
25
- expectType<string>(typed.message)
26
- expectType<400>(typed.statusCode)
27
-
28
- /* eslint-disable no-new */
29
- const CustomTypedArgError = createError<[string]>('OTHER_CODE', 'expect %s message', 400)
30
- CustomTypedArgError('a')
31
- expectError(CustomTypedArgError('a', 'b'))
32
- expectError(new CustomTypedArgError('a', 'b'))
33
- expectError(CustomTypedArgError(1))
34
- expectError(new CustomTypedArgError(1))
35
-
36
- const CustomTypedArgError2 = createError<string, number, [string]>('OTHER_CODE', 'expect %s message', 400)
37
- CustomTypedArgError2('a')
38
- expectError(CustomTypedArgError2('a', 'b'))
39
- expectError(new CustomTypedArgError2('a', 'b'))
40
- expectError(CustomTypedArgError2(1))
41
- expectError(new CustomTypedArgError2(1))
42
-
43
- const CustomTypedArgError3 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
44
- expectError(CustomTypedArgError3('a'))
45
- CustomTypedArgError3('a', 'b')
46
- new CustomTypedArgError3('a', 'b')
47
- expectError(CustomTypedArgError3(1))
48
- expectError(new CustomTypedArgError3(1))
49
- expectError(new CustomTypedArgError3(1, 2))
50
- expectError(new CustomTypedArgError3('1', 2))
51
- expectError(new CustomTypedArgError3(1, '2'))
52
-
53
- const CustomTypedArgError4 = createError<string, number, [string, string]>('OTHER_CODE', 'expect %s message but got %s', 400)
54
- expectError(CustomTypedArgError4('a'))
55
- CustomTypedArgError4('a', 'b')
56
- new CustomTypedArgError4('a', 'b')
57
- expectError(CustomTypedArgError4(1))
58
- expectError(new CustomTypedArgError4(1))
59
- expectError(new CustomTypedArgError4(1, 2))
60
- expectError(new CustomTypedArgError4('1', 2))
61
- expectError(new CustomTypedArgError4(1, '2'))
62
-
63
- const CustomTypedArgError5 = createError<[string, string, string, string]>('OTHER_CODE', 'expect %s message but got %s. Please contact %s by emailing to %s', 400)
64
- expectError(CustomTypedArgError5('a'))
65
- expectError(new CustomTypedArgError5('a', 'b'))
66
- expectError(new CustomTypedArgError5('a', 'b', 'c'))
67
- CustomTypedArgError5('a', 'b', 'c', 'd')
68
- expectError(new CustomTypedArgError5('a', 'b', 'c', 'd', 'e'))
69
-
70
- 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)
71
- expectError(CustomTypedArgError6('a'))
72
- expectError(new CustomTypedArgError6('a', 'b'))
73
- expectError(new CustomTypedArgError6('a', 'b', 'c'))
74
- CustomTypedArgError6('a', 'b', 'c', 'd')
75
- expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))
76
-
77
- const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError)
78
- expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor)
79
- CustomErrorWithErrorConstructor({ cause: new Error('Error') })
80
- const customErrorWithErrorConstructor = CustomErrorWithErrorConstructor()
81
- if (customErrorWithErrorConstructor instanceof FastifyError) {
82
- expectType<'ERROR_CODE'>(customErrorWithErrorConstructor.code)
83
- expectType<string>(customErrorWithErrorConstructor.message)
84
- expectType<500>(customErrorWithErrorConstructor.statusCode)
85
- }
86
-
87
- const error = new FastifyError('ERROR_CODE', 'message', 500)
88
- if (error instanceof FastifyError) {
89
- expectType<string>(error.code)
90
- expectType<string>(error.message)
91
- expectType<number | undefined>(error.statusCode)
92
- }