Skip to content

Error diagnostic channel always sends 200 unless explicitly changed #6409

@JPeer264

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions