From 286cea96037973d83213dd3bbad6dca8e727b8c0 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Sun, 28 Jul 2024 21:22:47 +0200 Subject: [PATCH] Test that logged simple handler errors has a stack --- test/handler.js | 1 + test/request.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/test/handler.js b/test/handler.js index 591534fb3..334a1a3d9 100755 --- a/test/handler.js +++ b/test/handler.js @@ -578,6 +578,7 @@ describe('handler', () => { expect(event.error.isBoom).to.equal(true); expect(event.error.output.statusCode).to.equal(403); expect(event.error.message).to.equal('Forbidden'); + expect(event.error.stack).to.exist(); }); }); diff --git a/test/request.js b/test/request.js index fa7b034e9..40e85ad32 100755 --- a/test/request.js +++ b/test/request.js @@ -973,6 +973,42 @@ describe('Request', () => { const res = await server.inject('/'); expect(res.statusCode).to.equal(500); }); + + it('logs thrown errors as boom errors', async () => { + + const server = Hapi.server({ debug: false }); + server.route({ + method: 'GET', + path: '/', + options: { + handler: function () { + + // eslint-disable-next-line no-undef + NOT_DEFINED_VAR; + } + } + }); + + const log = new Promise((resolve) => { + + server.events.on({ name: 'request', channels: 'internal' }, (request, event, tags) => { + + if (tags.handler && + tags.error) { + + resolve({ event, tags }); + } + }); + }); + + const res = await server.inject('/'); + expect(res.statusCode).to.equal(500); + + const { event } = await log; + expect(event.error.isBoom).to.equal(true); + expect(event.error.output.statusCode).to.equal(500); + expect(event.error.stack).to.exist(); + }); }); describe('_postCycle()', () => {