Skip to content

Commit 02ea033

Browse files
danbevMylesBorins
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. 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>
1 parent 68e78e8 commit 02ea033

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎lib/async_hooks.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ const {
4848
classAsyncHook{
4949
constructor({ init, before, after, destroy, promiseResolve }){
5050
if(init!==undefined&&typeofinit!=='function')
51-
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','init');
51+
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','hook.init');
5252
if(before!==undefined&&typeofbefore!=='function')
53-
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','before');
53+
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','hook.before');
5454
if(after!==undefined&&typeofafter!=='function')
55-
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','before');
55+
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','hook.after');
5656
if(destroy!==undefined&&typeofdestroy!=='function')
57-
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','before');
57+
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','hook.destroy');
5858
if(promiseResolve!==undefined&&typeofpromiseResolve!=='function')
59-
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','promiseResolve');
59+
thrownewerrors.TypeError('ERR_ASYNC_CALLBACK','hook.promiseResolve');
6060

6161
this[init_symbol]=init;
6262
this[before_symbol]=before;
Lines changed: 23 additions & 0 deletions
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+
constcommon=require('../common');
6+
constasync_hooks=require('async_hooks');
7+
constnon_function=10;
8+
9+
typeErrorForFunction('init');
10+
typeErrorForFunction('before');
11+
typeErrorForFunction('after');
12+
typeErrorForFunction('destroy');
13+
typeErrorForFunction('promiseResolve');
14+
15+
functiontypeErrorForFunction(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)