Home / File/ set-error-handler.test.js — fastify Source File

set-error-handler.test.js — fastify Source File

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

Entity Profile

Source Code

'use strict'

const { test } = require('node:test')
const Fastify = require('..')
const { FST_ERR_ERROR_HANDLER_NOT_FN, FST_ERR_ERROR_HANDLER_ALREADY_SET } = require('../lib/errors')

test('setErrorHandler should throw an error if the handler is not a function', t => {
  t.plan(1)
  const fastify = Fastify()

  t.assert.throws(() => fastify.setErrorHandler('not a function'), new FST_ERR_ERROR_HANDLER_NOT_FN())
})

test('setErrorHandler can be set independently in parent and child scopes', async t => {
  t.plan(1)

  const fastify = Fastify()

  t.assert.doesNotThrow(() => {
    fastify.setErrorHandler(() => {})
    fastify.register(async (child) => {
      child.setErrorHandler(() => {})
    })
  })
})

test('setErrorHandler can be overridden if allowErrorHandlerOverride is set to true', async t => {
  t.plan(2)

  const fastify = Fastify()
  t.after(() => fastify.close())

  fastify.register(async (child) => {
    child.setErrorHandler(() => {})
    t.assert.doesNotThrow(() => child.setErrorHandler(() => {}))
  })

  fastify.setErrorHandler(() => {})
  t.assert.doesNotThrow(() => fastify.setErrorHandler(() => {}))

  await fastify.ready()
})

test('if `allowErrorHandlerOverride` is disabled, setErrorHandler should throw if called more than once in the same scope', t => {
  t.plan(1)

  const fastify = Fastify({
    allowErrorHandlerOverride: false
  })

  fastify.setErrorHandler(() => {})
  t.assert.throws(() => fastify.setErrorHandler(() => {}), new FST_ERR_ERROR_HANDLER_ALREADY_SET())
})

test('if `allowErrorHandlerOverride` is disabled, setErrorHandler should throw if called more than once in the same scope 2', async t => {
  t.plan(1)

  const fastify = Fastify({
    allowErrorHandlerOverride: false
  })
  t.after(() => fastify.close())

  fastify.register(async (child) => {
    child.setErrorHandler(() => {})
    t.assert.throws(() => child.setErrorHandler(() => {}), new FST_ERR_ERROR_HANDLER_ALREADY_SET())
  })

  await fastify.ready()
})

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free