Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix error message in async_hooks constructor #19000

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class AsyncHook {
if (before !== undefined && typeof before !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about passing 'hook.before' as the option name? Usually a bare identifier means a function parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll take a look. Thanks

if (after !== undefined && typeof after !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'after');
if (destroy !== undefined && typeof destroy !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'before');
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'destroy');
if (promiseResolve !== undefined && typeof promiseResolve !== 'function')
throw new errors.TypeError('ERR_ASYNC_CALLBACK', 'promiseResolve');

Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-async-hooks-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'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;

common.expectsError(() => {
async_hooks.createHook({ init: non_function });
}, typeErrorForFunction('init'));

common.expectsError(() => {
async_hooks.createHook({ before: non_function });
}, typeErrorForFunction('before'));

common.expectsError(() => {
async_hooks.createHook({ after: non_function });
}, typeErrorForFunction('after'));

common.expectsError(() => {
async_hooks.createHook({ destroy: non_function });
}, typeErrorForFunction('destroy'));

common.expectsError(() => {
async_hooks.createHook({ promiseResolve: non_function });
}, typeErrorForFunction('promiseResolve'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeErrorForFunction could contain the common.expectsError logic as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good as well, I update this. Thanks


function typeErrorForFunction(functionName) {
return {
code: 'ERR_ASYNC_CALLBACK',
type: TypeError,
message: `${functionName} must be a function`,
};
}