Skip to content

Commit 8ddd4fa

Browse files
fix: internal function _addHook failure should be turned into the rejection that app.ready() is waiting for (#6105)
1 parent 850ebf6 commit 8ddd4fa

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

fastify.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,12 @@ function fastify (options) {
681681
this[kHooks].add(name, fn)
682682
} else {
683683
this.after((err, done) => {
684-
_addHook.call(this, name, fn)
685-
done(err)
684+
try {
685+
_addHook.call(this, name, fn)
686+
done(err)
687+
} catch (err) {
688+
done(err)
689+
}
686690
})
687691
}
688692
return this

test/internals/hooks.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { test } = require('node:test')
44
const { Hooks } = require('../../lib/hooks')
5+
const { default: fastify } = require('../../fastify')
56
const noop = () => {}
67

78
test('hooks should have 4 array with the registered hooks', t => {
@@ -77,3 +78,19 @@ test('should throw on wrong parameters', t => {
7778
t.assert.strictEqual(e.message, 'onSend hook should be a function, instead got [object Null]')
7879
}
7980
})
81+
82+
test('Integration test: internal function _addHook should be turned into app.ready() rejection', async (t) => {
83+
const app = fastify()
84+
85+
app.register(async function () {
86+
app.addHook('notRealHook', async () => {})
87+
})
88+
89+
try {
90+
await app.ready()
91+
t.assert.fail('Expected ready() to throw')
92+
} catch (err) {
93+
t.assert.strictEqual(err.code, 'FST_ERR_HOOK_NOT_SUPPORTED')
94+
t.assert.match(err.message, /hook not supported/i)
95+
}
96+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy