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

Code before:
const diagnostics = require('node:diagnostics_channel')

test('diagnostics channel error event should report correct status code', 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 () => {
    const err = new Error('test error')
    err.statusCode = 503
    throw err
  })

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

  t.assert.strictEqual(res.statusCode, 503)
  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({

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

Code after:
const diagnostics = require('node:diagnostics_channel')

test('diagnostics channel error event should report correct status code', 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 () => {
    const err = new Error('test error')
    err.statusCode = 503
    throw err
  })

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

  t.assert.strictEqual(res.statusCode, 503)
  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('/')

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