-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistencies with error handling #2860
Comments
From my read of this bug, it seems that throwing inside the error handler is not working as expected. I have a rough feeling we have not even designed things for this use case, and this is the result of several other fixes. It seems the clear example of something that we have not covered is the following: const f = fastify({ logger: true });
f.setErrorHandler((error, request, reply) => {
reply.send({ error: true });
throw new Error('error 2');
});
f.get('/test', (request, reply) => {
throw new Error('error 1');
}); In the issue you write:
What do you mean by propagating up the stack? Could you send a PR for implementing this behavior? I think this is the most important change. I would also note that throwing anything that we consider anything that is not error an antipattern. I'm not sure we should support this explicitly. |
As side note: I'm not sure this path has been tested in the lifecycle error flow: https://www.fastify.io/docs/latest/Lifecycle/ |
Since I've closed #2932 I'll reply here, since this is basically what @jsumners said as well:
I absolutely agree that this is an antipattern. But sometimes there are things not in your control and it's something that the language unfortunately allows. I personally think silently treating I think there should at least be consistency between As long as the language allows this and sources like MDN have this
we absolutely need to support this in my opinion. 'use strict';
const fastify = require('fastify')({
logger: true,
});
fastify.get('/sync-error', (request, reply) => {
throw new Error('/sync-error');
});
fastify.get('/sync-object', (request, reply) => {
throw { error: '/sync-object' };
});
fastify.get('/async-error', async (request, reply) => {
await new Promise((resolve, reject) => {
setImmediate(() => {
reject(new Error('/async-error'));
});
});
});
fastify.get('/async-object', async (request, reply) => {
await new Promise((resolve, reject) => {
setImmediate(() => {
reject({ path: '/async-object' });
});
});
});
fastify.listen(3000, (err) => {
if (err) {
fastify.log.error(err);
process.exit(1);
}
});
|
I'll think through this and work on it for the next major. This is something we should resolve before we cut v4. |
To reiterate on that: I'm using piscina inside a request handler and when the worker throws something that is a sublcass of |
I meant that if anything happens that is not supposed to (for example error thrown in error handler after the reply was already sent), the error should be let go its own way (eventually reaching unhandled exception/rejection) and not being swallowed by fastify. I already tried to investigate why this happens, but I find it a bit hard to navigate around without any comments and with that many different properties being used in every step. I will take a look again. |
We are missing the 2nd point: error handler of the parent plugin should catch errors throw by the children plugins. |
I'm preparing a PR to address encapsulation of error handlers and cascading errors between them. |
Closing as I think this was handled in #3261 |
馃悰 Bug Report
I noticed that errors are handled very inconsitently.
To Reproduce
Steps to reproduce the behavior:
In the following section we can see that throwing an error in the error handler is not supported when the handler is async, but the error handler is not, even thought I would understand as it is by reading the documentation (https://www.fastify.io/docs/latest/Errors/). On the other hand, if the error handler is async, it works properly (regardless of handler being sync or async).
In the following section we can see that in a sync handler only instanceof Error is regarded as an error, while in async handler everything that is thrown is regarded as an error.
fastify/lib/handleRequest.js
Lines 123 to 128 in 95bb213
fastify/lib/wrapThenable.js
Lines 32 to 42 in 95bb213
Expected behavior
In my opinion error handler should not handle errors that were thrown from within it since it's purpose is to handle errors that are sent to the handler and only a programmer error can happen within the handler itself (which cannot be handled). Therefore error (or rejection) of error handler should be propagated up the stack. I will open a new issue with an idea how to handle all these cases without introducing breaking changes. If I stick with the current state that fastify is in:
Your Environment
The text was updated successfully, but these errors were encountered: