Home / File/ route.4.test.js — fastify Source File

route.4.test.js — fastify Source File

Architecture documentation for route.4.test.js, a javascript file in the fastify codebase.

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('..')

test('route error handler overrides global custom error handler', async t => {
  t.plan(3)

  const fastify = Fastify()

  const customGlobalErrorHandler = (error, request, reply) => {
    t.assert.ifError(error)
    reply.code(429).send({ message: 'Too much coffee' })
  }

  const customRouteErrorHandler = (error, request, reply) => {
    t.assert.strictEqual(error.message, 'Wrong Pot Error')
    reply.code(418).send({
      message: 'Make a brew',
      statusCode: 418,
      error: 'Wrong Pot Error'
    })
  }

  fastify.setErrorHandler(customGlobalErrorHandler)

  fastify.route({
    method: 'GET',
    path: '/more-coffee',
    handler: (req, res) => {
      res.send(new Error('Wrong Pot Error'))
    },
    errorHandler: customRouteErrorHandler
  })

  const res = await fastify.inject({
    method: 'GET',
    url: '/more-coffee'
  })
  t.assert.strictEqual(res.statusCode, 418)
  t.assert.deepStrictEqual(JSON.parse(res.payload), {
    message: 'Make a brew',
    statusCode: 418,
    error: 'Wrong Pot Error'
  })
})

test('throws when route with empty url', async t => {
  t.plan(1)

  const fastify = Fastify()
  try {
    fastify.route({
      method: 'GET',
      url: '',
      handler: (req, res) => {
        res.send('hi!')
      }
    })
  } catch (err) {
// ... (68 more lines)

Frequently Asked Questions

What does route.4.test.js do?
route.4.test.js is a source file in the fastify codebase, written in javascript.
Where is route.4.test.js in the architecture?
route.4.test.js is located at test/route.4.test.js (directory: test).

Analyze Your Own Codebase

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

Try Supermodel Free