| 14 | }) |
| 15 | |
| 16 | function helper (code) { |
| 17 | test(class="st">'Reply error handling - code: ' + code, (t, testDone) => { |
| 18 | t.plan(4) |
| 19 | const fastify = Fastify() |
| 20 | t.after(() => fastify.close()) |
| 21 | const err = new Error(class="st">'winter is coming') |
| 22 | |
| 23 | fastify.get(class="st">'/', (req, reply) => { |
| 24 | reply |
| 25 | .code(Number(code)) |
| 26 | .send(err) |
| 27 | }) |
| 28 | |
| 29 | fastify.inject({ |
| 30 | method: class="st">'GET', |
| 31 | url: class="st">'/' |
| 32 | }, (error, res) => { |
| 33 | t.assert.ifError(error) |
| 34 | t.assert.strictEqual(res.statusCode, Number(code)) |
| 35 | t.assert.strictEqual(res.headers[class="st">'content-type'], class="st">'application/json; charset=utf-8') |
| 36 | t.assert.deepStrictEqual( |
| 37 | { |
| 38 | error: statusCodes[code], |
| 39 | message: err.message, |
| 40 | statusCode: Number(code) |
| 41 | }, |
| 42 | JSON.parse(res.payload) |
| 43 | ) |
| 44 | testDone() |
| 45 | }) |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | test(class="st">'preHandler hook error handling with external code', (t, testDone) => { |
| 50 | t.plan(3) |