diff --git a/lib/async_hooks.js b/lib/async_hooks.js index 46838304a39cfd..7718dad60a9cd2 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -48,15 +48,15 @@ const { class AsyncHook { constructor({ init, before, after, destroy, promiseResolve }) { if (init !== undefined && typeof init !== 'function') - throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'init'); + throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.init'); if (before !== undefined && typeof before !== 'function') - throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before'); + throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.before'); if (after !== undefined && typeof after !== 'function') - throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before'); + throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.after'); if (destroy !== undefined && typeof destroy !== 'function') - throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before'); + throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.destroy'); if (promiseResolve !== undefined && typeof promiseResolve !== 'function') - throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'promiseResolve'); + throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.promiseResolve'); this[init_symbol] = init; this[before_symbol] = before; diff --git a/test/parallel/test-async-hooks-constructor.js b/test/parallel/test-async-hooks-constructor.js new file mode 100644 index 00000000000000..f2b4df6a9f9f99 --- /dev/null +++ b/test/parallel/test-async-hooks-constructor.js @@ -0,0 +1,23 @@ +'use strict'; + +// This tests that AsyncHooks throws an error if bad parameters are passed. + +const common = require('../common'); +const async_hooks = require('async_hooks'); +const non_function = 10; + +typeErrorForFunction('init'); +typeErrorForFunction('before'); +typeErrorForFunction('after'); +typeErrorForFunction('destroy'); +typeErrorForFunction('promiseResolve'); + +function typeErrorForFunction(functionName) { + common.expectsError(() => { + async_hooks.createHook({ [functionName]: non_function }); + }, { + code: 'ERR_ASYNC_CALLBACK', + type: TypeError, + message: `hook.${functionName} must be a function` + }); +}