MCPcopy
hub / github.com/fastify/fastify / createNestedRoutes

Function createNestedRoutes

test/encapsulated-error-handler.test.js:150–174  ·  view source on GitHub ↗
(fastify, depth)

Source from the content-addressed store, hash-verified

148 * - GET /encapsulated
149 */
150 const createNestedRoutes = (fastify, depth) => {
151 if (depth < 0) {
152 throw new Error('Expected depth >= 0')
153 } else if (depth === 0) {
154 fastify.setErrorHandler(function a (err) {
155 // 3. innermost error handler catches the error, and throws a new error
156 t.assert.strictEqual(err.message, 'from_route')
157 throw new Error(`from_handler_${depth}`)
158 })
159 fastify.get('/encapsulated', async () => {
160 // 2. the endpoint throws an error
161 throw new Error('from_route')
162 })
163 } else {
164 fastify.setErrorHandler(function d (err) {
165 // 4 to {DEPTH+4}. error handlers each catch errors, and then throws a new error
166 t.assert.strictEqual(err.message, `from_handler_${depth - 1}`)
167 throw new Error(`from_handler_${depth}`)
168 })
169
170 fastify.register(async function (fastify) {
171 createNestedRoutes(fastify, depth - 1)
172 })
173 }
174 }
175
176 const fastify = Fastify()
177 createNestedRoutes(fastify, DEPTH)

Calls 1

setErrorHandlerMethod · 0.80

Tested by

no test coverage detected