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

Code before:
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({
    method: 'GET',
    url: '/'
  })

  // Note: The diagnostics channel fires before the custom error handler runs,
  // so it reports 500 (default) rather than 503 (set by custom handler).
  // This is expected behavior - the error channel reports the initial error state.
  t.assert.strictEqual(res.statusCode, 503)
  t.assert.strictEqual(diagnosticsStatusCode, 500, 'diagnostics channel reports status before custom handler')
  t.assert.notStrictEqual(diagnosticsStatusCode, res.statusCode, 'custom handler can change status after diagnostics')
})

test('Error.status property support', (t, done) => {
  t.plan(4)
  const fastify = Fastify()
  t.after(() => fastify.close())
  const err = new Error('winter is coming')
  err.status = 418

  diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
    t.assert.strictEqual(msg.error.message, 'winter is coming')
  })

  fastify.get('/', () => {
    return Promise.reject(err)
  })

  fastify.inject({

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

Code after:

  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,
  // so it reports 500 (default) rather than 503 (set by custom handler).
  // This is expected behavior - the error channel reports the initial error state.
  t.assert.strictEqual(res.statusCode, 503)
  t.assert.strictEqual(diagnosticsStatusCode, 500, 'diagnostics channel reports status before custom handler')
  t.assert.notStrictEqual(diagnosticsStatusCode, res.statusCode, 'custom handler can change status after diagnostics')
})

test('Error.status property support', (t, done) => {
  t.plan(4)
  const fastify = Fastify()
  t.after(() => fastify.close())
  const err = new Error('winter is coming')
  err.status = 418

  diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
    t.assert.strictEqual(msg.error.message, 'winter is coming')
  })

  fastify.get('/', () => {
    return Promise.reject(err)
  })

  fastify.inject({
    method: 'GET',
    url: '/'
  }, (error, res) => {
    t.assert.ifError(error)
    t.assert.strictEqual(res.statusCode, 418)
    t.assert.deepStrictEqual(
      {
        error: statusCodes['418'],
        message: err.message,