/
encapsulated-error-handler.test.js
50 lines (40 loc) · 1.28 KB
/
encapsulated-error-handler.test.js
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
'use strict'
const { test } = require('tap')
const Fastify = require('..')
test('encapuslates an error handler', async t => {
t.plan(3)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(async function a (err) {
t.equal(err.message, 'kaboom')
throw new Error('catched')
})
fastify.get('/encapsulated', async () => { throw new Error('kaboom') })
})
fastify.setErrorHandler(async function b (err) {
t.equal(err.message, 'catched')
throw new Error('wrapped')
})
const res = await fastify.inject('/encapsulated')
t.equal(res.json().message, 'wrapped')
})
test('onError hook nested', async t => {
t.plan(4)
const fastify = Fastify()
fastify.register(async function (fastify) {
fastify.setErrorHandler(async function a (err) {
t.equal(err.message, 'kaboom')
throw new Error('caught')
})
fastify.get('/encapsulated', async () => { throw new Error('kaboom') })
})
fastify.setErrorHandler(async function b (err) {
t.equal(err.message, 'caught')
throw new Error('wrapped')
})
fastify.addHook('onError', async function (request, reply, err) {
t.equal(err.message, 'kaboom')
})
const res = await fastify.inject('/encapsulated')
t.equal(res.json().message, 'wrapped')
})