-
-
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setErrorHandler is not called on plugin error #4204
Comments
I don't see how it will trigger the All yours request should go through |
My plugin will return an error for any route if it is not wrote in config. So for any route it will return an unauthorized error. (if route exist or not). |
Just one through, is it reproducible when using |
I don't think it's due to package manager because package is loaded without any error. |
It could cause by how package resolve since, It depends on how The below one is what I have done to minimize the repro. It works just fine with import Fastify from 'fastify'
import { Unauthorized } from 'unify-errors'
import unifyFastify from 'unify-fastify'
const fastify = Fastify()
await fastify.register(unifyFastify, { hideContextOnProd: false })
fastify.addHook('preValidation', function(request, reply, done) {
console.log('pre-validation hit')
throw new Unauthorized()
})
fastify.setNotFoundHandler(function() {
console.log('not found handler hit')
})
{
const { statusCode, payload } = await fastify.inject('/')
console.log(statusCode, payload)
} |
I have done the same thing with npm.
Same issue, so i can confirm that currently is not working. As I see on your example, your preValidation is not in a fastify plugin, so it doesn't work. A working example will be : import fastify from 'fastify';
import fp from 'fastify-plugin';
import { Unauthorized } from 'unify-errors';
import unifyFastify from 'unify-fastify';
(async () => {
const server = fastify();
await server.register(unifyFastify, { hideContextOnProd: false });
await server.register(
fp(async (f) => {
f.addHook('preValidation', async (request, reply) => {
console.log('pre-validation hit');
throw new Unauthorized();
});
}),
);
server.listen({ port: 3000 }).then(() => console.log('ready'));
})(); |
I have tried both using import Fastify from 'fastify'
import FastifyPlugin from 'fastify-plugin'
import { Unauthorized } from 'unify-errors'
import unifyFastify from 'unify-fastify'
const fastify = Fastify()
await fastify.register(unifyFastify, { hideContextOnProd: false })
await fastify.register(FastifyPlugin(function(fastify, _, done) {
fastify.addHook('preValidation', function(request, reply, done) {
console.log('pre-validation hit')
throw new Unauthorized()
})
done()
}))
fastify.setNotFoundHandler(function() {
console.log('not found handler hit')
})
{
const { statusCode, payload } = await fastify.inject('/')
console.log(statusCode, payload)
} |
I see the problem now. Without it, it doesn't. |
Okay i will push an update on unify-fastify to add it, but it seems anyway that there is a bug on fastify |
Can you include a reproduction without external modules? Thanks |
import fastify from 'fastify';
import fp from 'fastify-plugin';
(async () => {
const server = fastify();
await server.register(fp(async(f) => {
f.setErrorHandler((error, request, reply) => {
console.log("error handler is called");
reply.status(400).send("error");
})
}));
await server.register(
fp(async (f) => {
f.addHook('preValidation', async (request, reply) => {
console.log('pre-validation hit');
throw new Error('test');
});
}),
);
server.listen({ port: 3000 }).then(() => console.log('ready'));
})();
"error handler is called" will never be displayed |
The problem is more complicated than I though.
import Fastify from 'fastify'
const fastify = Fastify()
// if it is not await, it works fine
await fastify.register(async function(fastify) {
})
fastify.setErrorHandler(function(error, request, reply) {
return reply.code(401).send({"error":"Unauthorized"})
})
fastify.addHook('preValidation', async function(request, reply) {
console.log('pre-validation')
throw new Error()
})
{
const { statusCode, payload } = await fastify.inject('/')
console.log(statusCode, payload) // should be 401 {"error":"Unauthorized"}
}
import Fastify from 'fastify'
import FP from 'fastify-plugin'
const fastify = Fastify()
// it doesn't matter if this is awaited or not
fastify.register(FP(async function(fastify) {
fastify.setErrorHandler(function(error, request, reply) {
return reply.code(401).send({"error":"Unauthorized"})
})
}))
fastify.addHook('preValidation', async function(request, reply) {
console.log('pre-validation')
throw new Error()
})
// provide a empty not found handle works fine
// fastify.setNotFoundHandler()
{
const { statusCode, payload } = await fastify.inject('/')
console.log(statusCode, payload) // should be 401 {"error":"Unauthorized"}
} |
I have the same problem - throw an error in the async request decorator and I get the |
It's a different problem. Open a new issue with a minimum reproducible example. |
@climba03003 I have a fix coming for this. |
Prerequisites
Fastify version
4.5.0
Plugin version
4.2.0
Node.js version
16.17.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
22.04
Description
Hi,
When I try to return an error from fastify plugin, I see that setErrorHandler is not called.
I don't know why because I call my error handler plugin before to register my new plugin.
Thanks for the help !
Steps to Reproduce
pnpm dlx ts-node test/core/real.ts
Expected Behavior
Should return formated error from https://github.com/flexper/unify-fastify
The text was updated successfully, but these errors were encountered: