Skip to content

Commit 33bb93e

Browse files
committed
modify Readme
1 parent 164d4e2 commit 33bb93e

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ npm i @fastify/error
1515

1616
The module exports a function that you can use for consistent error objects, it takes 4 parameters:
1717

18-
```
18+
```js
1919
createError(code, message [, statusCode [, Base [, captureStackTrace]]])
2020
```
2121

@@ -58,6 +58,83 @@ new CustomError('world')
5858
new CustomError(1)
5959
```
6060

61+
### instanceof
62+
63+
All errors created with `createError` will be instances of the base error constructor you provided, or `Error` if none was provided.
64+
65+
```js
66+
const createError = require('@fastify/error')
67+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
68+
const customError = new CustomError('world')
69+
70+
console.log(customError instanceof CustomError) // true
71+
console.log(customError instanceof TypeError) // true
72+
console.log(customError instanceof Error) // true
73+
```
74+
75+
All instantiated errors will be instances of the `FastifyError` class. The `FastifyError` class can be required from the module directly.
76+
77+
```js
78+
const { createError, FastifyError } = require('@fastify/error')
79+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
80+
const customError = new CustomError('world')
81+
82+
console.log(customError instanceof FastifyError) // true
83+
```
84+
85+
It is possible to create a `FastifyError` that extends another `FastifyError`, created by `createError`, while instanceof working correctly.
86+
87+
```js
88+
const { createError, FastifyError } = require('@fastify/error')
89+
90+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
91+
const ChildCustomError = createError('CHILD_ERROR_CODE', 'Hello %s', 500, CustomError)
92+
93+
const customError = new ChildCustomError('world')
94+
95+
console.log(customError instanceof ChildCustomError) // true
96+
console.log(customError instanceof CustomError) // true
97+
console.log(customError instanceof FastifyError) // true
98+
console.log(customError instanceof TypeError) // true
99+
console.log(customError instanceof Error) // true
100+
```
101+
102+
If `fastify-error` is installed multiple times as direct and/or transitive dependency, it is ensured that Errors created with `createError` are working with the `instanceof` operator correctly cross the `fastify-error` installation, as long the code, e.g. `FST_ERR_CUSTOM_ERROR`, is the same.
103+
104+
```js
105+
const { createError, FastifyError } = require('@fastify/error')
106+
107+
// CustomError from `@fastify/some-plugin` is created with `createError` and
108+
// has its own `@fastify/error` installation as dependency. CustomError has
109+
// FST_ERR_CUSTOM_ERROR as code.
110+
const { CustomError: CustomErrorFromPlugin } = require('@fastify/some-plugin')
111+
112+
const CustomError = createError('FST_ERR_CUSTOM_ERROR', 'Hello %s', 500)
113+
114+
const customError = new CustomError('world')
115+
const customErrorFromPlugin = new CustomErrorFromPlugin('world')
116+
117+
console.log(customError instanceof CustomError) // true
118+
console.log(customError instanceof CustomErrorFromPlugin) // true
119+
console.log(customErrorFromPlugin instanceof CustomError) // true
120+
console.log(customErrorFromPlugin instanceof CustomErrorFromPlugin) // true
121+
```
122+
123+
Changing the code of an instantiated Error will not change the result of the `instanceof` operator.
124+
125+
```js
126+
const { createError, FastifyError } = require('@fastify/error')
127+
128+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
129+
const AnotherCustomError = createError('ANOTHER_ERROR_CODE', 'Hello %s', 500, CustomError)
130+
131+
const customError = new CustomError('world')
132+
customError.code = 'ANOTHER_ERROR_CODE'
133+
134+
console.log(customError instanceof CustomError) // true
135+
console.log(customError instanceof AnotherCustomError) // false
136+
```
137+
61138
## License
62139

63140
Licensed under [MIT](./LICENSE).

test/instanceof.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,55 @@ const os = require('node:os')
77
const test = require('node:test')
88
const { createError, FastifyError } = require('..')
99

10+
test('Readme: All errors created with `createError` will be instances of the base error constructor you provided, or `Error` if none was provided.', (t) => {
11+
t.plan(3)
12+
13+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
14+
const customError = new CustomError('world')
15+
16+
t.assert.ok(customError instanceof CustomError)
17+
t.assert.ok(customError instanceof TypeError)
18+
t.assert.ok(customError instanceof Error)
19+
})
20+
21+
test('Readme: All instantiated errors will be instances of the `FastifyError` class. The `FastifyError` class can be required from the module directly.', (t) => {
22+
t.plan(1)
23+
24+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
25+
const customError = new CustomError('world')
26+
27+
t.assert.ok(customError instanceof FastifyError)
28+
})
29+
30+
test('Readme: It is possible to create a `FastifyError` that extends another `FastifyError`, created by `createError`, while instanceof working correctly.', (t) => {
31+
t.plan(5)
32+
33+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
34+
const ChildCustomError = createError('CHILD_ERROR_CODE', 'Hello %s', 500, CustomError)
35+
36+
const customError = new ChildCustomError('world')
37+
38+
t.assert.ok(customError instanceof ChildCustomError)
39+
t.assert.ok(customError instanceof CustomError)
40+
t.assert.ok(customError instanceof FastifyError)
41+
t.assert.ok(customError instanceof TypeError)
42+
t.assert.ok(customError instanceof Error)
43+
})
44+
45+
test('Readme: Changing the code of an instantiated Error will not change the result of the `instanceof` operator.', (t) => {
46+
t.plan(3)
47+
48+
const CustomError = createError('ERROR_CODE', 'Hello %s', 500, TypeError)
49+
const AnotherCustomError = createError('ANOTHER_ERROR_CODE', 'Hello %s', 500, CustomError)
50+
51+
const customError = new CustomError('world')
52+
customError.code = 'ANOTHER_ERROR_CODE'
53+
54+
t.assert.ok(customError instanceof CustomError)
55+
t.assert.ok(customError instanceof AnotherCustomError === false)
56+
t.assert.ok(customError instanceof FastifyError)
57+
})
58+
1059
test('check if createError creates an Error which is instanceof Error', (t) => {
1160
t.plan(3)
1261

0 commit comments

Comments
 (0)