Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
|
Also, Furthermore, even if you only throw |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
I think most people using your package are unaware of the risk they're taking by assuming the types are correct. No, those codebases are NOT "perfectly fine", in fact they are not fine at all because they're built incorrectly. |
That looks like a very exaggerated statement to me. Anyway, I understand the safety concerns around misleading typing. @Eomm @mcollina |
Hold on, it's not just "some cases" of "mistaking" one type for another. It's all cases where you trust the type definitions to correctly tell you what type you're going to receive. Currently, it says you will receive
How exactly are Typescript developers supposed to "notice" that not all objects received in their handler have a I'm sorry, but I can't agree with what you're saying. It's absolutely obvious that the type definition is wrong and Typescript will do absolutely nothing to warn the developer about these hidden runtime errors. |
This comment was marked as outdated.
This comment was marked as outdated.
|
TypeScript and DefinitelyTyped do not offer any semver guarantees on anything. The way most breakages are handled in DefinitelyTyped is by patching all the dependents. You can read a good explanation here: Even for pure-TS packages, you can’t assume types are correct in any form. As a generic rule, if a type is incorrect and could lead to user bugs, I’m ok to change it (it’s a bug after all!) even if it causes downstream build errors. How big of a breakage are we looking at? Can somebody make a few examples? |
I must shamefully admit that I was unaware of this. |
|
My assessment is: I disagree with @paulius-valiunas opinion, that we should not allow typing error as Generics. Atleast not without atleast one additional utility function provided by us. If we have generics, then the developer will see that there is some potential issue with his setup. e.g. before: setErrorHandler(function (err, req, reply) {
if (err.code === 'FST_ERR_DOH') {
// everything fine
}
})setting err to unknown setErrorHandler(function (err, req, reply) {
if (err.code === 'FST_ERR_DOH') {
// err is unknown, no guarantee that it is an object with a code attribute. E.g. if it is for some reason
// null (is this even possible?)
}
})if it is a generic, than a developer can decide that he simply want to ignore it like before setErrorHandler<FastifyError>(function (err, req, reply) {
if (err.code === 'FST_ERR_DOH') {
// everything is fine again
}
})So it would take about 5 minutes to figure out how to restore the old behaviour. If we dont make it possible to pass FastifyError as generic, than we should consider to actually think how to solve it properly. Just nagging and to deny to configure the type of the error parameter without giving the right tools to the hand, means that a dev now has to find a "proper" solution. We could actually ensure that we always have a FastifyError. We just need to patch In the following lines we pass the received error: Line 64 in 6a428d9 Line 69 in 6a428d9 So we could check if error is of type FastifyError, and if not instantiate a new special FastifyError without stacktraces, where we assign the provided value as cause attribute. We could even patch an native Error to be shaped like a FastifyError. Maybe this is something we should implement in fastify-errors. A It makes sense to consider also the actual runtime issues, if you use the default error handler. Will we accidently expose critical information? throw 'the password is abc'Well, Typescript is basically breaking on every minor. It would be more honest if they would just had versioned it typescript v. 59.2 and not 5.9.2. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as outdated.
This comment was marked as outdated.
add generic to errorHandler
This comment was marked as off-topic.
This comment was marked as off-topic.
|
I marked some comments as outdated or off-topic to reduce the amount of text needed to read. I kept those, which have relevant content like technical arguments and final decisions and which are not containig repetitive arguments. I wrote a text into the first post, to be potentially used for the next release to give the devs some approaches to fix this properly. I also investigated our code and found a bug, that when |
There was a problem hiding this comment.
Having a generic here achieves 2 things:
- Allows folks who don't want to deal with the type change right away to easily fix compilation errors
- Makes typing easier in projects where only specific errors are thrown at runtime
These are OK reasons to have a generic that defaults to unknown. But I know for a fact that we will get many issues because of this, so we do need to present it as a notable change
Change in TypeScript typings for
setErrorHandleranderrorHandlerThe TypeScript typings of
setErrorHandleranderrorHandlerhave changed.The
errorparameter is now typed asunknown.Previously, it was assumed that
errorHandlerwould always receive aFastifyError. However, this assumption is unsafe because JavaScript allows throwing any value, not justErrorobjects. For example:This value could be passed into the
errorHandler. If your code then tries to access properties likeerror.codewhile assuming aFastifyError, it may result in runtime errors.To make this explicit, the
errorparameter is now typed asunknown.Example
Workarounds
You can force the old behavior by typing
erroryourself, but this bypasses the safety of the new typings:Using a generic (Not Recommended)
Typing the parameter directly (Not Recommended)
Type guard with property checks
codeproperty starts with'FST_'to determine if a value is a FastifyError.Recommended Approach
Use an
instanceofcheck to ensureerroris aFastifyError.This approach is safer and avoids assumptions:
@fastify/errorversion ≥ 4.2.0 to guaranteeinstanceofworks reliably.