Skip to content

Commit 58d787f

Browse files
authored
Fix crash when onReady hook throws (fastify#4579)
Signed-off-by: Matteo Collina <[email protected]>
1 parent 8605865 commit 58d787f

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/hooks.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,14 @@ function hookRunnerApplication (hookName, boot, server, cb) {
150150
return
151151
}
152152

153-
const ret = fn.call(server)
154-
if (ret && typeof ret.then === 'function') {
155-
ret.then(done, done)
156-
return
153+
try {
154+
const ret = fn.call(server)
155+
if (ret && typeof ret.then === 'function') {
156+
ret.then(done, done)
157+
return
158+
}
159+
} catch (error) {
160+
err = error
157161
}
158162

159163
done(err) // auto done

test/hooks.on-ready.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,16 @@ t.test('ready return registered', t => {
372372
.then(instance => { t.same(instance, fastify) })
373373
.catch(err => { t.fail(err) })
374374
})
375+
376+
t.test('do not crash with error in follow up onReady hook', async t => {
377+
const fastify = Fastify()
378+
379+
fastify.addHook('onReady', async function () {
380+
})
381+
382+
fastify.addHook('onReady', function () {
383+
throw new Error('kaboom')
384+
})
385+
386+
await t.rejects(fastify.ready())
387+
})

0 commit comments

Comments
 (0)