You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am writing my own error handler for Fastify and want to intercept ResponseValidationErrors similar to the code example here: #26 (comment). I'm using TypeScript.
When I try to check for the error using error instanceof ResponseValidationError I get false.
error.name === 'ResponseValidationError' is true so it is an instance of ResponseValidationError:
@kibertoad am I missing something fundamental with how instanceof works with classes? It looks like it's creating an instance of the class I linked so I'd expect error instanceof ResponseValidationError to be true. I'm starting to doubt I understand what's going on!
If you import a library A, and another library B imports a library A, and you use a class instantiated by library B using class from library A, there is a high chance they will be of a different realm, and won't pass instanceof check. for this reason I highly advise never to rely on instanceof for classes coming from external libraries, but use custom typeguards instead.
I am writing my own error handler for Fastify and want to intercept
ResponseValidationError
s similar to the code example here: #26 (comment). I'm using TypeScript.When I try to check for the error using
error instanceof ResponseValidationError
I getfalse
.error.name === 'ResponseValidationError'
is true so it is an instance of ResponseValidationError:fastify-type-provider-zod/src/index.ts
Line 122 in 85675fa
Am I missing something here? If the name is set correctly I'd expect the
instanceof
check to work.My overall goal is to respond normally on response validation errors and log instead of returning errors to the client.
Code to reproduce below.
The text was updated successfully, but these errors were encountered: