Repository: fastify/fastify
File: test/diagnostics-channel/error-status.test.js (JavaScript)

Code before:
  t.assert.strictEqual(diagnosticsStatusCode, 503, 'diagnostics channel should report correct status code')
  t.assert.strictEqual(diagnosticsStatusCode, res.statusCode, 'diagnostics status should match response status')
})

test('diagnostics channel error event should report 500 for errors without status', async (t) => {
  t.plan(3)
  const fastify = Fastify()
  t.after(() => fastify.close())

  let diagnosticsStatusCode

  const channel = diagnostics.channel('tracing:fastify.request.handler:error')
  const handler = (msg) => {
    diagnosticsStatusCode = msg.reply.statusCode
  }
  channel.subscribe(handler)
  t.after(() => channel.unsubscribe(handler))

  fastify.get('/', async () => {
    throw new Error('plain error without status')
  })

  const res = await fastify.inject({
    method: 'GET',
    url: '/'
  })

  t.assert.strictEqual(res.statusCode, 500)
  t.assert.strictEqual(diagnosticsStatusCode, 500, 'diagnostics channel should report 500 for plain errors')
  t.assert.strictEqual(diagnosticsStatusCode, res.statusCode, 'diagnostics status should match response status')
})

test('diagnostics channel error event should report correct status with custom error handler', async (t) => {
  t.plan(3)
  const fastify = Fastify()
  t.after(() => fastify.close())

  let diagnosticsStatusCode

  const channel = diagnostics.channel('tracing:fastify.request.handler:error')
  const handler = (msg) => {
    diagnosticsStatusCode = msg.reply.statusCode
  }
  channel.subscribe(handler)
  t.after(() => channel.unsubscribe(handler))

  fastify.setErrorHandler((error, request, reply) => {
    reply.status(503).send({ error: error.message })
  })

  fastify.get('/', async () => {

Review comment (suggestion):
```suggestion
  const res = await fastify.inject('/')
```

Code after:

test('diagnostics channel error event should report 500 for errors without status', async (t) => {
  t.plan(3)
  const fastify = Fastify()
  t.after(() => fastify.close())

  let diagnosticsStatusCode

  const channel = diagnostics.channel('tracing:fastify.request.handler:error')
  const handler = (msg) => {
    diagnosticsStatusCode = msg.reply.statusCode
  }
  channel.subscribe(handler)
  t.after(() => channel.unsubscribe(handler))

  fastify.get('/', async () => {
    throw new Error('plain error without status')
  })

  const res = await fastify.inject('/')

  t.assert.strictEqual(res.statusCode, 500)
  t.assert.strictEqual(diagnosticsStatusCode, 500, 'diagnostics channel should report 500 for plain errors')
  t.assert.strictEqual(diagnosticsStatusCode, res.statusCode, 'diagnostics status should match response status')
})

test('diagnostics channel error event should report correct status with custom error handler', async (t) => {
  t.plan(3)
  const fastify = Fastify()
  t.after(() => fastify.close())

  let diagnosticsStatusCode

  const channel = diagnostics.channel('tracing:fastify.request.handler:error')
  const handler = (msg) => {
    diagnosticsStatusCode = msg.reply.statusCode
  }
  channel.subscribe(handler)
  t.after(() => channel.unsubscribe(handler))

  fastify.setErrorHandler((error, request, reply) => {
    reply.status(503).send({ error: error.message })
  })

  fastify.get('/', async () => {
    throw new Error('handler error')
  })

  const res = await fastify.inject('/')

  // Note: The diagnostics channel fires before the custom error handler runs,