Skip to content

Commit

Permalink
src: fix error message in async_hooks constructor
Browse files Browse the repository at this point in the history
There are two minor issues in the AsyncHook constructor, if the object
passed in has an after and/or destroy property that are not functions
the errors thrown will still be:
TypeError [ERR_ASYNC_CALLBACK]: before must be a function

This commit updates the code and adds a unit test.

Backport-PR-URL: #22380
PR-URL: #19000
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matheus Marchini <matheus@sthima.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
  • Loading branch information
danbev authored and MylesBorins committed Sep 6, 2018
1 parent 68e78e8 commit 02ea033
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-async-hooks-constructor.js
Original file line number Diff line number Diff line change
@@ -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`
});
}

0 comments on commit 02ea033

Please sign in to comment.