Skip to content

Commit 4d5cd5c

Browse files
danbevaddaleax
authored andcommitted
src: fix error message in async_hooks constructor
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. PR-URL: #19000 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matheus Marchini <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent 71d09ec commit 4d5cd5c

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/async_hooks.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ const {
4848
class AsyncHook {
4949
constructor({ init, before, after, destroy, promiseResolve }) {
5050
if (init !== undefined && typeof init !== 'function')
51-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'init');
51+
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.init');
5252
if (before !== undefined && typeof before !== 'function')
53-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
53+
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.before');
5454
if (after !== undefined && typeof after !== 'function')
55-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
55+
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.after');
5656
if (destroy !== undefined && typeof destroy !== 'function')
57-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
57+
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.destroy');
5858
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
59-
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'promiseResolve');
59+
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'hook.promiseResolve');
6060

6161
this[init_symbol] = init;
6262
this[before_symbol] = before;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
// This tests that AsyncHooks throws an error if bad parameters are passed.
4+
5+
const common = require('../common');
6+
const async_hooks = require('async_hooks');
7+
const non_function = 10;
8+
9+
typeErrorForFunction('init');
10+
typeErrorForFunction('before');
11+
typeErrorForFunction('after');
12+
typeErrorForFunction('destroy');
13+
typeErrorForFunction('promiseResolve');
14+
15+
function typeErrorForFunction(functionName) {
16+
common.expectsError(() => {
17+
async_hooks.createHook({ [functionName]: non_function });
18+
}, {
19+
code: 'ERR_ASYNC_CALLBACK',
20+
type: TypeError,
21+
message: `hook.${functionName} must be a function`
22+
});
23+
}

0 commit comments

Comments
 (0)