Skip to content

Commit 5bacc86

Browse files
committed
feat: implement instanceof
1 parent d669b15 commit 5bacc86

4 files changed

Lines changed: 60 additions & 2 deletions

File tree

index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

5+
const FastifyErrorSymbol = Symbol.for('fastify-error')
6+
57
function toString () {
68
return `${this.name} [${this.code}]: ${this.message}`
79
}
@@ -38,7 +40,13 @@ function createError (code, message, statusCode = 500, Base = Error, captureStac
3840
enumerable: false,
3941
writable: true,
4042
configurable: true
41-
}
43+
},
44+
[FastifyErrorSymbol]: {
45+
value: true,
46+
enumerable: false,
47+
writable: false,
48+
configurable: false
49+
},
4250
})
4351

4452
FastifyError.prototype[Symbol.toStringTag] = 'Error'
@@ -50,6 +58,16 @@ function createError (code, message, statusCode = 500, Base = Error, captureStac
5058

5159
createError.captureStackTrace = true
5260

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+
})
69+
5370
module.exports = createError
71+
module.exports.FastifyError = FastifyErrorConstructor
5472
module.exports.default = createError
5573
module.exports.createError = createError

test/index.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const test = require('node:test')
4-
const createError = require('..')
4+
const { createError, FastifyError } = require('..')
55

66
test('Create error with zero parameter', (t) => {
77
t.plan(6)
@@ -221,3 +221,28 @@ test('Create an error with last argument null', (t) => {
221221
t.assert.ok(err instanceof Error)
222222
t.assert.ifError(err.cause)
223223
})
224+
225+
test('check if instanceof works', (t) => {
226+
t.plan(5)
227+
228+
const NewError = createError('CODE', 'Not available')
229+
const err = NewError()
230+
231+
const NewFastifySyntaxError = createError('CODE', 'Not available', 500, SyntaxError)
232+
const syntaxErr = NewFastifySyntaxError()
233+
234+
t.assert.ok(err instanceof Error)
235+
t.assert.ok(err instanceof FastifyError)
236+
t.assert.ok(new Error() instanceof FastifyError === false)
237+
t.assert.ok(syntaxErr instanceof SyntaxError)
238+
t.assert.ok(syntaxErr instanceof FastifyError)
239+
})
240+
241+
test('check if FastifyError is instantiable', (t) => {
242+
t.plan(2)
243+
244+
const err = new FastifyError()
245+
246+
t.assert.ok(err instanceof FastifyError)
247+
t.assert.ok(err instanceof Error)
248+
})

types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ declare namespace createError {
4040
readonly prototype: FastifyError & E
4141
}
4242

43+
export const FastifyError: FastifyErrorConstructor
44+
4345
export const createError: CreateError
4446
export { createError as default }
4547
}

types/index.test-d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,16 @@ expectError(new CustomTypedArgError6('a', 'b', 'c', 'd', 'e'))
7777
const CustomErrorWithErrorConstructor = createError('ERROR_CODE', 'message', 500, TypeError)
7878
expectType<FastifyErrorConstructor<{ code: 'ERROR_CODE', statusCode: 500 }>>(CustomErrorWithErrorConstructor)
7979
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+
}

0 commit comments

Comments
 (0)