'use strict'
const { test } = require('node:test')
const Fastify = require('..')
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) {
t.assert.strictEqual(err.message, 'from_endpoint')
throw new Error('from_inner')
})
fastify.get('/encapsulated', async () => {
throw new Error('from_endpoint')
})
})
fastify.setErrorHandler(async function b (err) {
t.assert.strictEqual(err.message, 'from_inner')
throw new Error('from_outer')
})
const res = await fastify.inject('/encapsulated')
t.assert.strictEqual(res.json().message, 'from_outer')
})
test('encapsulates a synchronous error handler', async t => {
t.plan(3)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(function a (err) {
t.assert.strictEqual(err.message, 'from_endpoint')
throw new Error('from_inner')
})
fastify.get('/encapsulated', async () => {
throw new Error('from_endpoint')
})
})
fastify.setErrorHandler(async function b (err) {
t.assert.strictEqual(err.message, 'from_inner')
throw new Error('from_outer')
})
const res = await fastify.inject('/encapsulated')
t.assert.strictEqual(res.json().message, 'from_outer')
})
test('onError hook nested', async t => {
t.plan(4)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(async function a (err) {
t.assert.strictEqual(err.message, 'from_endpoint')
throw new Error('from_inner')
})
fastify.get('/encapsulated', async () => {
throw new Error('from_endpoint')
})
})
fastify.setErrorHandler(async function b (err) {
t.assert.strictEqual(err.message, 'from_inner')
throw new Error('from_outer')
})
fastify.addHook('onError', async function (request, reply, err) {
t.assert.strictEqual(err.message, 'from_endpoint')
})
const res = await fastify.inject('/encapsulated')
t.assert.strictEqual(res.json().message, 'from_outer')
})
test('encapuslates an error handler, for errors thrown in hooks', async t => {
t.plan(3)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(function a (err) {
t.assert.strictEqual(err.message, 'from_hook')
throw new Error('from_inner')
})
fastify.addHook('onRequest', async () => {
throw new Error('from_hook')
})
fastify.get('/encapsulated', async () => {})
})
fastify.setErrorHandler(function b (err) {
t.assert.strictEqual(err.message, 'from_inner')
throw new Error('from_outer')
})
const res = await fastify.inject('/encapsulated')
t.assert.strictEqual(res.json().message, 'from_outer')
})
test('encapuslates many synchronous error handlers that rethrow errors', async t => {
const DEPTH = 100
t.plan(DEPTH + 2)
* This creates a very nested set of error handlers, that looks like:
* plugin
* - error handler
* - plugin
* - error handler
* - plugin
* ... {to DEPTH levels}
* - plugin
* - error handler
* - GET /encapsulated
*/
const createNestedRoutes = (fastify, depth) => {
if (depth < 0) {
throw new Error('Expected depth >= 0')
} else if (depth === 0) {
fastify.setErrorHandler(function a (err) {
t.assert.strictEqual(err.message, 'from_route')
throw new Error(`from_handler_${depth}`)
})
fastify.get('/encapsulated', async () => {
throw new Error('from_route')
})
} else {
fastify.setErrorHandler(function d (err) {
t.assert.strictEqual(err.message, `from_handler_${depth - 1}`)
throw new Error(`from_handler_${depth}`)
})
fastify.register(async function (fastify) {
createNestedRoutes(fastify, depth - 1)
})
}
}
const fastify = Fastify()
createNestedRoutes(fastify, DEPTH)
const res = await fastify.inject('/encapsulated')
t.assert.strictEqual(res.json().message, `from_handler_${DEPTH}`)
})
test('encapuslates many asynchronous error handlers that rethrow errors', async t => {
const DEPTH = 100
t.plan(DEPTH + 2)
* This creates a very nested set of error handlers, that looks like:
* plugin
* - error handler
* - plugin
* - error handler
* - plugin
* ... {to DEPTH levels}
* - plugin
* - error handler
* - GET /encapsulated
*/
const createNestedRoutes = (fastify, depth) => {
if (depth < 0) {
throw new Error('Expected depth >= 0')
} else if (depth === 0) {
fastify.setErrorHandler(async function a (err) {
t.assert.strictEqual(err.message, 'from_route')
throw new Error(`from_handler_${depth}`)
})
fastify.get('/encapsulated', async () => {
throw new Error('from_route')
})
} else {
fastify.setErrorHandler(async function m (err) {
t.assert.strictEqual(err.message, `from_handler_${depth - 1}`)
throw new Error(`from_handler_${depth}`)
})
fastify.register(async function (fastify) {
createNestedRoutes(fastify, depth - 1)
})
}
}
const fastify = Fastify()
createNestedRoutes(fastify, DEPTH)
const res = await fastify.inject('/encapsulated')
t.assert.strictEqual(res.json().message, `from_handler_${DEPTH}`)
})