Skip to content

Commit af87e7b

Browse files
authored
feat: add hint which plugin is requesting specific decorator (fastify#2778)
1 parent 9b0c854 commit af87e7b

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

Diff for: lib/pluginUtils.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,22 @@ function checkDecorators (fn) {
6767
const meta = getMeta(fn)
6868
if (!meta) return
6969

70-
const decorators = meta.decorators
70+
const { decorators, name } = meta
7171
if (!decorators) return
7272

73-
if (decorators.fastify) _checkDecorators.call(this, 'Fastify', decorators.fastify)
74-
if (decorators.reply) _checkDecorators.call(this[kReply], 'Reply', decorators.reply)
75-
if (decorators.request) _checkDecorators.call(this[kRequest], 'Request', decorators.request)
73+
if (decorators.fastify) _checkDecorators.call(this, 'Fastify', decorators.fastify, name)
74+
if (decorators.reply) _checkDecorators.call(this[kReply], 'Reply', decorators.reply, name)
75+
if (decorators.request) _checkDecorators.call(this[kRequest], 'Request', decorators.request, name)
7676
}
7777

78-
function _checkDecorators (instance, decorators) {
78+
function _checkDecorators (instance, decorators, name) {
7979
assert(Array.isArray(decorators), 'The decorators should be an array of strings')
8080

8181
decorators.forEach(decorator => {
82+
const withPluginName = typeof name === 'string' ? ` required by '${name}'` : ''
8283
assert(
8384
instance === 'Fastify' ? decorator in this : decorator in this.prototype,
84-
`The decorator '${decorator}' is not present in ${instance}`
85+
`The decorator '${decorator}'${withPluginName} is not present in ${instance}`
8586
)
8687
})
8788
}

Diff for: test/plugin.test.js

+55
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,61 @@ test('plugin metadata - decorators', t => {
725725
}
726726
})
727727

728+
test('plugin metadata - decorators - should throw', t => {
729+
t.plan(1)
730+
const fastify = Fastify()
731+
732+
fastify.decorate('plugin1', true)
733+
fastify.decorateReply('plugin1', true)
734+
735+
plugin[Symbol.for('skip-override')] = true
736+
plugin[Symbol.for('plugin-meta')] = {
737+
decorators: {
738+
fastify: ['plugin1'],
739+
reply: ['plugin1'],
740+
request: ['plugin1']
741+
}
742+
}
743+
744+
fastify.register(plugin)
745+
fastify.ready((err) => {
746+
t.equals(err.message, "The decorator 'plugin1' is not present in Request")
747+
})
748+
749+
function plugin (instance, opts, next) {
750+
instance.decorate('plugin', true)
751+
next()
752+
}
753+
})
754+
755+
test('plugin metadata - decorators - should throw with plugin name', t => {
756+
t.plan(1)
757+
const fastify = Fastify()
758+
759+
fastify.decorate('plugin1', true)
760+
fastify.decorateReply('plugin1', true)
761+
762+
plugin[Symbol.for('skip-override')] = true
763+
plugin[Symbol.for('plugin-meta')] = {
764+
name: 'the-plugin',
765+
decorators: {
766+
fastify: ['plugin1'],
767+
reply: ['plugin1'],
768+
request: ['plugin1']
769+
}
770+
}
771+
772+
fastify.register(plugin)
773+
fastify.ready((err) => {
774+
t.equals(err.message, "The decorator 'plugin1' required by 'the-plugin' is not present in Request")
775+
})
776+
777+
function plugin (instance, opts, next) {
778+
instance.decorate('plugin', true)
779+
next()
780+
}
781+
})
782+
728783
test('plugin metadata - dependencies', t => {
729784
t.plan(1)
730785
const fastify = Fastify()

0 commit comments

Comments
 (0)