From 96745c1881853df9c0efacbf19de21ea1ee65cf3 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Mon, 31 Oct 2022 18:55:00 +0530 Subject: [PATCH 1/3] Improve error message for hooks check --- lib/hooks.js | 2 +- lib/route.js | 4 ++-- test/hooks.test.js | 2 +- test/internals/hooks.test.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/hooks.js b/lib/hooks.js index d1bdadad12..ea9a5148f9 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, fn === null ? null : typeof fn) } Hooks.prototype.add = function (hook, fn) { diff --git a/lib/route.js b/lib/route.js index fc4526de0f..835ab6d970 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, func === null ? null : typeof 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, opts[hook] === null ? null : typeof opts[hook]) } } } diff --git a/test/hooks.test.js b/test/hooks.test.js index ceaf5ada4d..df1164eb7e 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -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 null')) // undefined is ok fastify.route({ diff --git a/test/internals/hooks.test.js b/test/internals/hooks.test.js index f5aed5d644..32f0c08a6d 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 null') } }) From ded543f8af65ad4338f7ea75591891aad31b2b54 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Mon, 31 Oct 2022 20:16:11 +0530 Subject: [PATCH 2/3] log null value more precisely --- lib/hooks.js | 2 +- lib/route.js | 4 ++-- test/hooks.test.js | 2 +- test/internals/hooks.test.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/hooks.js b/lib/hooks.js index ea9a5148f9..e5b2dfd82e 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, fn === null ? null : typeof fn) + if (typeof fn !== 'function') throw new FST_ERR_HOOK_INVALID_HANDLER(hook, fn === null ? Object.prototype.toString.call(fn) : typeof fn) } Hooks.prototype.add = function (hook, fn) { diff --git a/lib/route.js b/lib/route.js index 835ab6d970..0687fcc60d 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, func === null ? null : typeof func) + throw new FST_ERR_HOOK_INVALID_HANDLER(hook, func === null ? Object.prototype.toString.call(func) : typeof fn) } } } else if (opts[hook] !== undefined && typeof opts[hook] !== 'function') { - throw new FST_ERR_HOOK_INVALID_HANDLER(hook, opts[hook] === null ? null : typeof opts[hook]) + throw new FST_ERR_HOOK_INVALID_HANDLER(hook, opts[hook] === null ? Object.prototype.toString.call(opts[hook]) : typeof opts[hook]) } } } diff --git a/test/hooks.test.js b/test/hooks.test.js index df1164eb7e..d53146505e 100644 --- a/test/hooks.test.js +++ b/test/hooks.test.js @@ -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 null')) + }, new Error('onRequest hook should be a function, instead got [object Null]')) // undefined is ok fastify.route({ diff --git a/test/internals/hooks.test.js b/test/internals/hooks.test.js index 32f0c08a6d..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 null') + t.equal(e.message, 'onSend hook should be a function, instead got [object Null]') } }) From 5d786cb1fc3c3c915e2125395d371554adbba951 Mon Sep 17 00:00:00 2001 From: Debadutta Panda Date: Mon, 31 Oct 2022 20:42:15 +0530 Subject: [PATCH 3/3] final update and all testcase passed --- lib/hooks.js | 2 +- lib/route.js | 4 ++-- test/hooks.test.js | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/hooks.js b/lib/hooks.js index e5b2dfd82e..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, fn === null ? Object.prototype.toString.call(fn) : 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 0687fcc60d..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, func === null ? Object.prototype.toString.call(func) : typeof fn) + 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, opts[hook] === null ? Object.prototype.toString.call(opts[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 d53146505e..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({ @@ -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]')) })