Skip to content

Commit 4e6a67e

Browse files
committed
why not?
1 parent 21c11fc commit 4e6a67e

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

index.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
const { format } = require('node:util')
44

5-
const FastifyErrorSymbol = Symbol.for('fastify-error')
6-
75
function toString () {
86
return `${this.name} [${this.code}]: ${this.message}`
97
}
108

9+
const FastifyGenericErrorSymbol = Symbol.for('fastify-error-generic')
10+
1111
function createError (code, message, statusCode = 500, Base = Error, captureStackTrace = createError.captureStackTrace) {
12+
const shouldCreateFastifyGenericError = code === FastifyGenericErrorSymbol
13+
14+
if (shouldCreateFastifyGenericError) {
15+
code = 'FST_ERR'
16+
}
17+
1218
if (!code) throw new Error('Fastify error code must not be empty')
1319
if (!message) throw new Error('Fastify error message must not be empty')
1420

1521
code = code.toUpperCase()
1622
!statusCode && (statusCode = undefined)
1723

24+
const FastifySpecificErrorSymbol = Symbol.for(`fastify-error ${code}`)
25+
1826
function FastifyError (...args) {
1927
if (!new.target) {
2028
return new FastifyError(...args)
@@ -41,14 +49,40 @@ function createError (code, message, statusCode = 500, Base = Error, captureStac
4149
writable: true,
4250
configurable: true
4351
},
44-
[FastifyErrorSymbol]: {
52+
[FastifyGenericErrorSymbol]: {
4553
value: true,
4654
enumerable: false,
4755
writable: false,
4856
configurable: false
4957
},
58+
[FastifySpecificErrorSymbol]: {
59+
value: true,
60+
enumerable: false,
61+
writable: false,
62+
configurable: false
63+
}
5064
})
5165

66+
if (shouldCreateFastifyGenericError) {
67+
Object.defineProperty(FastifyError, Symbol.hasInstance, {
68+
value (instance) {
69+
return instance && instance[FastifyGenericErrorSymbol]
70+
},
71+
configurable: false,
72+
writable: false,
73+
enumerable: false
74+
})
75+
} else {
76+
Object.defineProperty(FastifyError, Symbol.hasInstance, {
77+
value (instance) {
78+
return instance && instance[FastifySpecificErrorSymbol]
79+
},
80+
configurable: false,
81+
writable: false,
82+
enumerable: false
83+
})
84+
}
85+
5286
FastifyError.prototype[Symbol.toStringTag] = 'Error'
5387

5488
FastifyError.prototype.toString = toString
@@ -58,14 +92,7 @@ function createError (code, message, statusCode = 500, Base = Error, captureStac
5892

5993
createError.captureStackTrace = true
6094

61-
const FastifyErrorConstructor = createError('FST_ERR', 'Fastify Error', 500, Error)
62-
Object.defineProperty(FastifyErrorConstructor, Symbol.hasInstance, {
63-
value: function (instance) {
64-
return instance && instance[FastifyErrorSymbol]
65-
},
66-
configurable: true,
67-
enumerable: false
68-
})
95+
const FastifyErrorConstructor = createError(FastifyGenericErrorSymbol, 'Fastify Error', 500, Error)
6996

7097
module.exports = createError
7198
module.exports.FastifyError = FastifyErrorConstructor

test/instanceof.test.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test('check if createError creates an Error with the right BaseConstructor, whic
5656
})
5757

5858
test('instanceof', async (t) => {
59-
const assertsPlanned = 2
59+
const assertsPlanned = 5
6060
t.plan(assertsPlanned)
6161

6262
const testCwd = path.resolve(os.tmpdir())
@@ -88,9 +88,10 @@ test('instanceof', async (t) => {
8888
const { createError } = require('fastify-error')
8989
9090
const Boom = createError('Boom', 'Boom', 500)
91+
const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
9192
9293
module.exports.foo = function foo () {
93-
throw new Boom('foo go Boom')
94+
throw new ChildBoom('foo go Boom')
9495
}
9596
`)
9697

@@ -112,12 +113,17 @@ test('instanceof', async (t) => {
112113
const { foo } = require('main')
113114
114115
const Boom = createError('Boom', 'Boom', 500)
116+
const ChildBoom = createError('ChildBoom', 'Boom', 500, Boom)
117+
const NotChildBoom = createError('NotChildBoom', 'NotChildBoom', 500, Boom)
115118
116119
try {
117120
foo()
118121
} catch (err) {
122+
process.send(err instanceof Error)
119123
process.send(err instanceof FastifyError)
124+
process.send(err instanceof NotChildBoom)
120125
process.send(err instanceof Boom)
126+
process.send(err instanceof ChildBoom)
121127
}
122128
`)
123129

@@ -134,7 +140,7 @@ test('instanceof', async (t) => {
134140

135141
const child = cp.fork(path.resolve(testCwd, 'index.js'), {
136142
cwd: testCwd,
137-
stdio: 'pipe',
143+
stdio: 'inherit',
138144
env: {
139145
...process.env,
140146
NODE_OPTIONS: '--no-warnings'
@@ -146,10 +152,19 @@ test('instanceof', async (t) => {
146152
try {
147153
switch (messageCount) {
148154
case 0:
149-
t.assert.strictEqual(message, true, 'instanceof FastifyError')
155+
t.assert.strictEqual(message, true, 'instanceof Error')
150156
break
151157
case 1:
152-
t.assert.strictEqual(message, false, 'instanceof Boom')
158+
t.assert.strictEqual(message, true, 'instanceof FastifyError')
159+
break
160+
case 2:
161+
t.assert.strictEqual(message, false, 'instanceof NotChildBoom')
162+
break
163+
case 3:
164+
t.assert.strictEqual(message, true, 'instanceof Boom')
165+
break
166+
case 4:
167+
t.assert.strictEqual(message, true, 'instanceof ChildBoom')
153168
break
154169
}
155170
if (++messageCount === assertsPlanned) {

0 commit comments

Comments
 (0)