Skip to content

Error diagnostic channel always sends 200 unless explicitly changed #6409

Closed

Description

@JPeer264

Prerequisites

  • I have written a descriptive issue title
  • 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):

const diagnosticsChannel = require('node:diagnostics_channel');
const Fastify = require('fastify')

diagnosticsChannel.subscribe('tracing:fastify.request.handler:error', ({ reply }) => {
  console.log('diagnosticsChannel status code', reply.statusCode);
});

// Instantiate the framework
const fastify = Fastify({
  logger: true
})

// Register parent error handler
fastify.setErrorHandler((error, request, reply) => {
  reply.status(500).send({ ok: false })
})

fastify.register((app, options, next) => {
  // Register child error handler
  fastify.setErrorHandler((error, request, reply) => {
    throw error
  })

  fastify.get('/good', async (request, reply) => {
    // Throws an Error instance, 'bar'
    throw new Error('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.

FWIW following works as expected:

fastify.get('/500', async (request, reply) => {
  reply.status(500)
  throw new Error('500 error')
})

Link to code that reproduces the bug

https://github.com/JPeer264/fastify-error-repro

Expected Behavior

No response

Activity

  1. tt-a1i commented on Dec 11, 2025

    @tt-a1i
    Contributor

    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:

    1. New channel - Add something like error-handled that fires after the error handler runs
    2. Documentation - Clarify that reply.statusCode in the error channel may not reflect the final response status
    3. 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!

  2. jsumners commented on Dec 11, 2025

    @jsumners
    Member

    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.

    A PR to resolve this would be welcome.

  3. added
    bug
    on Dec 11, 2025
  4. JPeer264 commented on Jan 15, 2026

    @JPeer264
    Author

    FWIW the issue still exists with error codes other than 500. Let's say you set it to 418 in the setErrorHandler:

    fastify.setErrorHandler((error, request, reply) => {
      reply.status(418).send({ ok: false })
    })

    Then it will also return a 500 in the channel, but returns with a 418 status code

  5. github-actions commented on Sep 1, 2026

    @github-actions

    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.

  6. locked as resolved and limited conversation to collaborators on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions