Skip to content

Commit

Permalink
errors: add more information in case of invalid callbacks
Browse files Browse the repository at this point in the history
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 <luigipinca@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
  • Loading branch information
BridgeAR committed Apr 4, 2019
1 parent a9bf665 commit 3b04496
Show file tree
Hide file tree
Showing 32 changed files with 90 additions and 73 deletions.
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
if (options === null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

debug('%s renegotiate()',
this._tlsOptions.isServer ? 'server' : 'client',
Expand Down
6 changes: 3 additions & 3 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function lookup(hostname, options, callback) {
callback = options;
family = 0;
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
} else if (options !== null && typeof options === 'object') {
hints = options.hints >>> 0;
family = options.family >>> 0;
Expand Down Expand Up @@ -174,7 +174,7 @@ function lookupService(hostname, port, callback) {
throw new ERR_SOCKET_BAD_PORT(port);

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

port = +port;

Expand Down Expand Up @@ -213,7 +213,7 @@ function resolver(bindingName) {

validateString(name, 'name');
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

const req = new QueryReqWrap();
Expand Down
8 changes: 4 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ function maybeCallback(cb) {
if (typeof cb === 'function')
return cb;

throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(cb);
}

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

return (...args) => {
Expand All @@ -158,7 +158,7 @@ function makeCallback(cb) {
// transformed anyway.
function makeStatsCallback(cb) {
if (typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(cb);
}

return (err, stats) => {
Expand Down Expand Up @@ -1749,7 +1749,7 @@ function copyFile(src, dest, flags, callback) {
callback = flags;
flags = 0;
} else if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

src = toPathIfFileURL(src);
Expand Down
2 changes: 1 addition & 1 deletion lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Session extends EventEmitter {
throw new ERR_INVALID_ARG_TYPE('params', 'Object', params);
}
if (callback && typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

if (!this[connectionSymbol]) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function generateKeyPair(type, options, callback) {
const impl = check(type, options);

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

const wrap = new AsyncWrap(Providers.KEYPAIRGENREQUEST);
wrap.ondone = (ex, pubkey, privkey) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) {
check(password, salt, iterations, keylen, digest));

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

const encoding = getDefaultEncoding();
const keybuf = Buffer.alloc(keylen);
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function assertSize(size, elementSize, offset, length) {
function randomBytes(size, cb) {
size = assertSize(size, 1, 0, Infinity);
if (cb !== undefined && typeof cb !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(cb);

const buf = Buffer.alloc(size);

Expand Down Expand Up @@ -95,7 +95,7 @@ function randomFill(buf, offset, size, cb) {
cb = size;
size = buf.byteLength - offset;
} else if (typeof cb !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(cb);
}

offset = assertOffset(offset, elementSize, buf.byteLength);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/crypto/scrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function scrypt(password, salt, keylen, options, callback = defaults) {
({ password, salt, keylen } = options);

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

const encoding = getDefaultEncoding();
const keybuf = Buffer.alloc(keylen);
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,8 @@ E('ERR_INVALID_ARG_VALUE', (name, value, reason = 'is invalid') => {
E('ERR_INVALID_ASYNC_ID', 'Invalid %s value: %s', RangeError);
E('ERR_INVALID_BUFFER_SIZE',
'Buffer size must be a multiple of %s', RangeError);
E('ERR_INVALID_CALLBACK', 'Callback must be a function', TypeError);
E('ERR_INVALID_CALLBACK',
'Callback must be a function. Received %O', TypeError);
E('ERR_INVALID_CHAR',
// Using a default argument here is important so the argument is not counted
// towards `Function#length`.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ class Http2ServerResponse extends Stream {

createPushResponse(headers, callback) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
if (this[kState].closed) {
process.nextTick(callback, new ERR_HTTP2_INVALID_STREAM());
return;
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ class Http2Session extends EventEmitter {
throw new ERR_HTTP2_PING_LENGTH();
}
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

const cb = pingCallback(callback);
if (this.connecting || this.closed) {
Expand Down Expand Up @@ -1148,7 +1148,7 @@ class Http2Session extends EventEmitter {
validateSettings(settings);

if (callback && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
debug(`Http2Session ${sessionName(this[kType])}: sending settings`);

this[kState].pendingAck++;
Expand Down Expand Up @@ -1900,7 +1900,7 @@ class Http2Stream extends Duplex {
if (code < 0 || code > kMaxInt)
throw new ERR_OUT_OF_RANGE('code', `>= 0 && <= ${kMaxInt}`, code);
if (callback !== undefined && typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

if (this.closed)
return;
Expand Down Expand Up @@ -2256,7 +2256,7 @@ class ServerHttp2Stream extends Http2Stream {
}

if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

assertIsObject(options, 'options');
options = { ...options };
Expand Down Expand Up @@ -2690,7 +2690,7 @@ class Http2SecureServer extends TLSServer {
this.timeout = msecs;
if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
this.on('timeout', callback);
}
return this;
Expand All @@ -2711,7 +2711,7 @@ class Http2Server extends NETServer {
this.timeout = msecs;
if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
this.on('timeout', callback);
}
return this;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process/task_queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class TickObject {
// exit since the callback would not have a chance to be executed.
function nextTick(callback) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);

if (process._exiting)
return;
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ function setStreamTimeout(msecs, callback) {
if (msecs === 0) {
if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
this.removeListener('timeout', callback);
}
} else {
Expand All @@ -223,7 +223,7 @@ function setStreamTimeout(msecs, callback) {

if (callback !== undefined) {
if (typeof callback !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
this.once('timeout', callback);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function popCallback(streams) {
// a single stream. Therefore optimize for the average case instead of
// checking for length === 0 as well.
if (typeof streams[streams.length - 1] !== 'function')
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(streams[streams.length - 1]);
return streams.pop();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function insert(item, refed, start) {
function setUnrefTimeout(callback, after) {
// Type checking identical to setTimeout()
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

const timer = new Timeout(callback, after, undefined, false);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ defineIDLClass(URLSearchParams.prototype, 'URLSearchParams', {
throw new ERR_INVALID_THIS('URLSearchParams');
}
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

let list = this[searchParams];
Expand Down
2 changes: 1 addition & 1 deletion lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ let gcTrackingIsEnabled = false;
class PerformanceObserver extends AsyncResource {
constructor(callback) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}
super('PerformanceObserver');
Object.defineProperties(this, {
Expand Down
6 changes: 3 additions & 3 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function enroll(item, msecs) {

function setTimeout(callback, after, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

var i, args;
Expand Down Expand Up @@ -168,7 +168,7 @@ function clearTimeout(timer) {

function setInterval(callback, repeat, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

var i, args;
Expand Down Expand Up @@ -255,7 +255,7 @@ const Immediate = class Immediate {

function setImmediate(callback, arg1, arg2, arg3) {
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
throw new ERR_INVALID_CALLBACK(callback);
}

var i, args;
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ if (!common.hasCrypto)
const assert = require('assert');
const crypto = require('crypto');
const { kMaxLength } = require('buffer');
const { inspect } = require('util');

const kMaxUint32 = Math.pow(2, 32) - 1;
const kMaxPossibleLength = Math.min(kMaxLength, kMaxUint32);
Expand Down Expand Up @@ -292,7 +293,7 @@ assert.throws(
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError,
message: 'Callback must be a function',
message: `Callback must be a function. Received ${inspect(i)}`
});
});

Expand All @@ -302,7 +303,7 @@ assert.throws(
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError,
message: 'Callback must be a function',
message: `Callback must be a function. Received ${inspect(i)}`
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ test(new Uint8Array(expected.length),
assert.throws(
() => fs.read(fd, Buffer.alloc(1), 0, 1, 0),
{
message: 'Callback must be a function',
message: 'Callback must be a function. Received undefined',
code: 'ERR_INVALID_CALLBACK',
}
);
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-http2-client-rststream-before-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const h2 = require('http2');
const { inspect } = require('util');

const server = h2.createServer();
server.on('stream', (stream) => {
Expand Down Expand Up @@ -35,7 +36,7 @@ server.listen(0, common.mustCall(() => {
{
type: TypeError,
code: 'ERR_INVALID_CALLBACK',
message: 'Callback must be a function'
message: `Callback must be a function. Received ${inspect(notFunction)}`
}
);
assert.strictEqual(req.closed, false);
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-client-settings-before-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const h2 = require('http2');
const { inspect } = require('util');

const server = h2.createServer();

Expand Down Expand Up @@ -51,7 +52,8 @@ server.listen(0, common.mustCall(() => {
{
type: TypeError,
code: 'ERR_INVALID_CALLBACK',
message: 'Callback must be a function'
message:
`Callback must be a function. Received ${inspect(invalidCallback)}`
}
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const server = h2.createServer((request, response) => {
{
code: 'ERR_INVALID_CALLBACK',
type: TypeError,
message: 'Callback must be a function'
message: 'Callback must be a function. Received undefined'
}
);

Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-http2-ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (!common.hasCrypto)
const async_hooks = require('async_hooks');
const assert = require('assert');
const http2 = require('http2');
const { inspect } = require('util');

const pings = new Set();
const events = [0, 0, 0, 0];
Expand Down Expand Up @@ -119,7 +120,8 @@ server.listen(0, common.mustCall(() => {
{
type: TypeError,
code: 'ERR_INVALID_CALLBACK',
message: 'Callback must be a function'
message: 'Callback must be a function. ' +
`Received ${inspect(invalidCallback)}`
}
)
);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http2-server-push-stream-errors-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ server.on('stream', common.mustCall((stream, headers) => {
}, {}, 'callback'),
{
code: 'ERR_INVALID_CALLBACK',
message: 'Callback must be a function'
message: "Callback must be a function. Received 'callback'"
}
);

Expand Down
Loading

0 comments on commit 3b04496

Please sign in to comment.