Home / File/ error-status.test.js — fastify Source File

error-status.test.js — fastify Source File

Architecture documentation for error-status.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('../..')
const statusCodes = require('node:http').STATUS_CODES
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')
  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) => {
// ... (64 more lines)

Frequently Asked Questions

What does error-status.test.js do?
error-status.test.js is a source file in the fastify codebase, written in javascript.
Where is error-status.test.js in the architecture?
error-status.test.js is located at test/diagnostics-channel/error-status.test.js (directory: test/diagnostics-channel).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free