From a71dd83cc65c79c2a01dfbe91f8ee64fec3c0509 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Mon, 31 Oct 2022 23:04:07 +0530 Subject: [PATCH] fix: Improve error message for hooks check (#4387) * Improve error message for hooks check * log null value more precisely * final update and all testcase passed --- lib/hooks.js | 2 +- lib/route.js | 4 ++-- test/hooks.test.js | 6 +++--- test/internals/hooks.test.js | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/hooks.js b/lib/hooks.js index d1bdadad12..3125478177 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -52,7 +52,7 @@ Hooks.prototype.validate = function (hook, fn) { if (supportedHooks.indexOf(hook) === -1) { throw new Error(`${hook} hook not supported!`) } - if (typeof fn !== 'function') throw new FST_ERR_HOOK_INVALID_HANDLER(hook, typeof fn) + if (typeof fn !== 'function') throw new FST_ERR_HOOK_INVALID_HANDLER(hook, Object.prototype.toString.call(fn)) } Hooks.prototype.add = function (hook, fn) { diff --git a/lib/route.js b/lib/route.js index fc4526de0f..621aa871a1 100644 --- a/lib/route.js +++ b/lib/route.js @@ -249,11 +249,11 @@ function buildRouting (options) { if (Array.isArray(opts[hook])) { for (const func of opts[hook]) { if (typeof func !== 'function') { - throw new FST_ERR_HOOK_INVALID_HANDLER(hook, typeof func) + throw new FST_ERR_HOOK_INVALID_HANDLER(hook, Object.prototype.toString.call(func)) } } } else if (opts[hook] !== undefined && typeof opts[hook] !== 'function') { - throw new FST_ERR_HOOK_INVALID_HANDLER(hook, typeof opts[hook]) + throw new FST_ERR_HOOK_INVALID_HANDLER(hook, Object.prototype.toString.call(opts[hook])) } } } diff --git a/test/hooks.test.js b/test/hooks.test.js index ceaf5ada4d..70d589a7aa 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -3332,7 +3332,7 @@ test('registering invalid hooks should throw an error', async t => { return 'hello world' } }) - }, new Error('onRequest hook should be a function, instead got undefined')) + }, new Error('onRequest hook should be a function, instead got [object Undefined]')) t.throws(() => { fastify.route({ @@ -3343,7 +3343,7 @@ test('registering invalid hooks should throw an error', async t => { return 'hello world' } }) - }, new Error('onRequest hook should be a function, instead got object')) + }, new Error('onRequest hook should be a function, instead got [object Null]')) // undefined is ok fastify.route({ @@ -3363,5 +3363,5 @@ test('registering invalid hooks should throw an error', async t => { fastify.get('/', function (request, reply) { reply.send('hello world') }) - }, new Error('onSend hook should be a function, instead got undefined')) + }, new Error('onSend hook should be a function, instead got [object Undefined]')) }) diff --git a/test/internals/hooks.test.js b/test/internals/hooks.test.js index f5aed5d644..5db3854078 100644 --- a/test/internals/hooks.test.js +++ b/test/internals/hooks.test.js @@ -78,6 +78,6 @@ test('should throw on wrong parameters', t => { t.fail() } catch (e) { t.equal(e.code, 'FST_ERR_HOOK_INVALID_HANDLER') - t.equal(e.message, 'onSend hook should be a function, instead got object') + t.equal(e.message, 'onSend hook should be a function, instead got [object Null]') } })