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 have searched existing issues to ensure the bug has not already been reported
Fastify version
5.6.2
Plugin version
No response
Node.js version
22.21.1
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
15.6 (24G84)
Description
We were getting an issue that our SDK always returns a 200 on the diagnostics channel. I was digging a little into it and I am not quite sure if this is intended or a bug.
When you run following code the status is always 200, even though the status should be 500 (I took it from here and only added the diagnostics channel):
constdiagnosticsChannel=require('node:diagnostics_channel');constFastify=require('fastify')diagnosticsChannel.subscribe('tracing:fastify.request.handler:error',({ reply })=>{console.log('diagnosticsChannel status code',reply.statusCode);});// Instantiate the frameworkconstfastify=Fastify({logger: true})// Register parent error handlerfastify.setErrorHandler((error,request,reply)=>{reply.status(500).send({ok: false})})fastify.register((app,options,next)=>{// Register child error handlerfastify.setErrorHandler((error,request,reply)=>{throwerror})fastify.get('/good',async(request,reply)=>{// Throws an Error instance, 'bar'thrownewError('bar')})next()})
In this case the setErrorHandler is called way later than the diagnostics channel is sending the error, that is why the status code is also 200.
Now I wonder if it is intended that it works that way, since on the one hand the error is emitted right when the error happens, on the other hand the reply is actually not quite accurate to what actually happened.
Took a look at the code - the timing is intentional but maybe not ideal for this use case.
The tracing:fastify.request.handler:error channel publishes right when the error is caught, before reply.send(err) triggers the error handler chain:
// lib/handle-request.js}catch(err){channels.error.publish(store)// publishes here (statusCode still 200)reply.send(err)// error handler runs after this}
The status code only gets set later in defaultErrorHandler() when it calls reply.code(500).
So this seems to be by design - the channel fires at the moment of error detection, not after error handling completes. But I can see how this is confusing for monitoring tools like Sentry that expect the final status code.
A few possible directions:
New channel - Add something like error-handled that fires after the error handler runs
Documentation - Clarify that reply.statusCode in the error channel may not reflect the final response status
Change behavior - Move the publish after error handling (but this might break existing consumers)
Would any of these make sense? Happy to help with a PR if needed!
This seems like an overlooked scenario and a missed test. I would expect the reply object in the channel to be the same as the one provided to the onRequest hook.
Activity
tt-a1i commentedon Dec 11, 2025
Took a look at the code - the timing is intentional but maybe not ideal for this use case.
The
tracing:fastify.request.handler:errorchannel publishes right when the error is caught, beforereply.send(err)triggers the error handler chain:The status code only gets set later in
defaultErrorHandler()when it callsreply.code(500).So this seems to be by design - the channel fires at the moment of error detection, not after error handling completes. But I can see how this is confusing for monitoring tools like Sentry that expect the final status code.
A few possible directions:
error-handledthat fires after the error handler runsreply.statusCodein the error channel may not reflect the final response statusWould any of these make sense? Happy to help with a PR if needed!
jsumners commentedon Dec 11, 2025
This seems like an overlooked scenario and a missed test. I would expect the
replyobject in the channel to be the same as the one provided to theonRequesthook.A PR to resolve this would be welcome.
JPeer264 commentedon Jan 15, 2026
FWIW the issue still exists with error codes other than
500. Let's say you set it to 418 in thesetErrorHandler:Then it will also return a
500in the channel, but returns with a418status codegithub-actions commentedon Sep 1, 2026
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.