encapsulated-error-handler.test.js — fastify Source File
Architecture documentation for encapsulated-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('..')
// Because of how error handlers wrap things, following the control flow can be tricky
// In this test file numbered comments indicate the order statements are expected to execute
test('encapsulates an asynchronous error handler', async t => {
t.plan(3)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(async function a (err) {
// 3. the inner error handler catches the error, and throws a new error
t.assert.strictEqual(err.message, 'from_endpoint')
throw new Error('from_inner')
})
fastify.get('/encapsulated', async () => {
// 2. the endpoint throws an error
throw new Error('from_endpoint')
})
})
fastify.setErrorHandler(async function b (err) {
// 4. the outer error handler catches the error thrown by the inner error handler
t.assert.strictEqual(err.message, 'from_inner')
// 5. the outer error handler throws a new error
throw new Error('from_outer')
})
// 1. the endpoint is called
const res = await fastify.inject('/encapsulated')
// 6. the default error handler returns the error from the outer error handler
t.assert.strictEqual(res.json().message, 'from_outer')
})
// See discussion in https://github.com/fastify/fastify/pull/5222#discussion_r1432573655
test('encapsulates a synchronous error handler', async t => {
t.plan(3)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(function a (err) {
// 3. the inner error handler catches the error, and throws a new error
t.assert.strictEqual(err.message, 'from_endpoint')
throw new Error('from_inner')
})
fastify.get('/encapsulated', async () => {
// 2. the endpoint throws an error
throw new Error('from_endpoint')
})
})
fastify.setErrorHandler(async function b (err) {
// 4. the outer error handler catches the error thrown by the inner error handler
t.assert.strictEqual(err.message, 'from_inner')
// 5. the outer error handler throws a new error
throw new Error('from_outer')
})
// ... (178 more lines)
Source
Frequently Asked Questions
What does encapsulated-error-handler.test.js do?
encapsulated-error-handler.test.js is a source file in the fastify codebase, written in javascript.
Where is encapsulated-error-handler.test.js in the architecture?
encapsulated-error-handler.test.js is located at test/encapsulated-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