Skip to content

Commit 3b04496

Browse files
committed
errors: add more information in case of invalid callbacks
This adds the actual callback that is passed through to the error message in case an ERR_INVALID_CALLBACK error is thrown. PR-URL: #27048 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent a9bf665 commit 3b04496

32 files changed

+90
-73
lines changed

lib/_tls_wrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
633633
if (options === null || typeof options !== 'object')
634634
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
635635
if (callback !== undefined && typeof callback !== 'function')
636-
throw new ERR_INVALID_CALLBACK();
636+
throw new ERR_INVALID_CALLBACK(callback);
637637

638638
debug('%s renegotiate()',
639639
this._tlsOptions.isServer ? 'server' : 'client',

lib/dns.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function lookup(hostname, options, callback) {
9898
callback = options;
9999
family = 0;
100100
} else if (typeof callback !== 'function') {
101-
throw new ERR_INVALID_CALLBACK();
101+
throw new ERR_INVALID_CALLBACK(callback);
102102
} else if (options !== null && typeof options === 'object') {
103103
hints = options.hints >>> 0;
104104
family = options.family >>> 0;
@@ -174,7 +174,7 @@ function lookupService(hostname, port, callback) {
174174
throw new ERR_SOCKET_BAD_PORT(port);
175175

176176
if (typeof callback !== 'function')
177-
throw new ERR_INVALID_CALLBACK();
177+
throw new ERR_INVALID_CALLBACK(callback);
178178

179179
port = +port;
180180

@@ -213,7 +213,7 @@ function resolver(bindingName) {
213213

214214
validateString(name, 'name');
215215
if (typeof callback !== 'function') {
216-
throw new ERR_INVALID_CALLBACK();
216+
throw new ERR_INVALID_CALLBACK(callback);
217217
}
218218

219219
const req = new QueryReqWrap();

lib/fs.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ function maybeCallback(cb) {
137137
if (typeof cb === 'function')
138138
return cb;
139139

140-
throw new ERR_INVALID_CALLBACK();
140+
throw new ERR_INVALID_CALLBACK(cb);
141141
}
142142

143143
// Ensure that callbacks run in the global context. Only use this function
144144
// for callbacks that are passed to the binding layer, callbacks that are
145145
// invoked from JS already run in the proper scope.
146146
function makeCallback(cb) {
147147
if (typeof cb !== 'function') {
148-
throw new ERR_INVALID_CALLBACK();
148+
throw new ERR_INVALID_CALLBACK(cb);
149149
}
150150

151151
return (...args) => {
@@ -158,7 +158,7 @@ function makeCallback(cb) {
158158
// transformed anyway.
159159
function makeStatsCallback(cb) {
160160
if (typeof cb !== 'function') {
161-
throw new ERR_INVALID_CALLBACK();
161+
throw new ERR_INVALID_CALLBACK(cb);
162162
}
163163

164164
return (err, stats) => {
@@ -1749,7 +1749,7 @@ function copyFile(src, dest, flags, callback) {
17491749
callback = flags;
17501750
flags = 0;
17511751
} else if (typeof callback !== 'function') {
1752-
throw new ERR_INVALID_CALLBACK();
1752+
throw new ERR_INVALID_CALLBACK(callback);
17531753
}
17541754

17551755
src = toPathIfFileURL(src);

lib/inspector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Session extends EventEmitter {
7878
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
7979
}
8080
if (callback && typeof callback !== 'function') {
81-
throw new ERR_INVALID_CALLBACK();
81+
throw new ERR_INVALID_CALLBACK(callback);
8282
}
8383

8484
if (!this[connectionSymbol]) {

lib/internal/crypto/keygen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function generateKeyPair(type, options, callback) {
4646
const impl = check(type, options);
4747

4848
if (typeof callback !== 'function')
49-
throw new ERR_INVALID_CALLBACK();
49+
throw new ERR_INVALID_CALLBACK(callback);
5050

5151
const wrap = new AsyncWrap(Providers.KEYPAIRGENREQUEST);
5252
wrap.ondone = (ex, pubkey, privkey) => {

lib/internal/crypto/pbkdf2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) {
2626
check(password, salt, iterations, keylen, digest));
2727

2828
if (typeof callback !== 'function')
29-
throw new ERR_INVALID_CALLBACK();
29+
throw new ERR_INVALID_CALLBACK(callback);
3030

3131
const encoding = getDefaultEncoding();
3232
const keybuf = Buffer.alloc(keylen);

lib/internal/crypto/random.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function assertSize(size, elementSize, offset, length) {
4747
function randomBytes(size, cb) {
4848
size = assertSize(size, 1, 0, Infinity);
4949
if (cb !== undefined && typeof cb !== 'function')
50-
throw new ERR_INVALID_CALLBACK();
50+
throw new ERR_INVALID_CALLBACK(cb);
5151

5252
const buf = Buffer.alloc(size);
5353

@@ -95,7 +95,7 @@ function randomFill(buf, offset, size, cb) {
9595
cb = size;
9696
size = buf.byteLength - offset;
9797
} else if (typeof cb !== 'function') {
98-
throw new ERR_INVALID_CALLBACK();
98+
throw new ERR_INVALID_CALLBACK(cb);
9999
}
100100

101101
offset = assertOffset(offset, elementSize, buf.byteLength);

lib/internal/crypto/scrypt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function scrypt(password, salt, keylen, options, callback = defaults) {
3232
({ password, salt, keylen } = options);
3333

3434
if (typeof callback !== 'function')
35-
throw new ERR_INVALID_CALLBACK();
35+
throw new ERR_INVALID_CALLBACK(callback);
3636

3737
const encoding = getDefaultEncoding();
3838
const keybuf = Buffer.alloc(keylen);

lib/internal/errors.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,8 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
854854
E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
855855
E('ERR_INVALID_BUFFER_SIZE',
856856
'Buffer size must be a multiple of %s', RangeError);
857-
E('ERR_INVALID_CALLBACK', 'Callback must be a function', TypeError);
857+
E('ERR_INVALID_CALLBACK',
858+
'Callback must be a function. Received %O', TypeError);
858859
E('ERR_INVALID_CHAR',
859860
// Using a default argument here is important so the argument is not counted
860861
// towards `Function#length`.

lib/internal/http2/compat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ class Http2ServerResponse extends Stream {
701701

702702
createPushResponse(headers, callback) {
703703
if (typeof callback !== 'function')
704-
throw new ERR_INVALID_CALLBACK();
704+
throw new ERR_INVALID_CALLBACK(callback);
705705
if (this[kState].closed) {
706706
process.nextTick(callback, new ERR_HTTP2_INVALID_STREAM());
707707
return;

0 commit comments

Comments
 (0)