File size: 8,397 Bytes
23ac194
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
'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')
  })

  // 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')
})

test('onError hook nested', async t => {
  t.plan(4)

  const fastify = Fastify()
  fastify.register(async function (fastify) {
    fastify.setErrorHandler(async function a (err) {
      // 4. 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) {
    // 5. the outer error handler catches the error thrown by the inner error handler
    t.assert.strictEqual(err.message, 'from_inner')
    // 6. the outer error handler throws a new error
    throw new Error('from_outer')
  })

  fastify.addHook('onError', async function (request, reply, err) {
    // 3. the hook receives the error
    t.assert.strictEqual(err.message, 'from_endpoint')
  })

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // 7. the default error handler returns the error from the outer error handler
  t.assert.strictEqual(res.json().message, 'from_outer')
})

// See https://github.com/fastify/fastify/issues/5220
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) {
      // 3. the inner error handler catches the error, and throws a new error
      t.assert.strictEqual(err.message, 'from_hook')
      throw new Error('from_inner')
    })
    fastify.addHook('onRequest', async () => {
      // 2. the hook throws an error
      throw new Error('from_hook')
    })
    fastify.get('/encapsulated', async () => {})
  })

  fastify.setErrorHandler(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 https://github.com/fastify/fastify/issues/5220
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) {
        // 3. innermost error handler catches the error, and throws a new error
        t.assert.strictEqual(err.message, 'from_route')
        throw new Error(`from_handler_${depth}`)
      })
      fastify.get('/encapsulated', async () => {
        // 2. the endpoint throws an error
        throw new Error('from_route')
      })
    } else {
      fastify.setErrorHandler(function d (err) {
        // 4 to {DEPTH+4}. error handlers each catch errors, and then throws a new error
        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)

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // {DEPTH+5}. the default error handler returns the error from the outermost error handler
  t.assert.strictEqual(res.json().message, `from_handler_${DEPTH}`)
})

// See https://github.com/fastify/fastify/issues/5220
// This was not failing previously, but we want to make sure the behavior continues to work in the same way across async and sync handlers
// Plus, the current setup is somewhat fragile to tweaks to wrapThenable as that's what retries (by calling res.send(err) again)
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) {
        // 3. innermost error handler catches the error, and throws a new error
        t.assert.strictEqual(err.message, 'from_route')
        throw new Error(`from_handler_${depth}`)
      })
      fastify.get('/encapsulated', async () => {
        // 2. the endpoint throws an error
        throw new Error('from_route')
      })
    } else {
      fastify.setErrorHandler(async function m (err) {
        // 4 to {DEPTH+4}. error handlers each catch errors, and then throws a new error
        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)

  // 1. the endpoint is called
  const res = await fastify.inject('/encapsulated')
  // {DEPTH+5}. the default error handler returns the error from the outermost error handler
  t.assert.strictEqual(res.json().message, `from_handler_${DEPTH}`)
})