Skip to content

Commit

Permalink
timers: support name in validateTimerDuration()
Browse files Browse the repository at this point in the history
Allow passing a name to validateTimerDuration() so that error
messages can reflect the name of the thing being validated.

PR-URL: #26215
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
cjihrig committed Feb 22, 2019
1 parent 4804a18 commit a1d05e4
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
}

listenSocketTimeout(this);
msecs = validateTimerDuration(msecs);
msecs = validateTimerDuration(msecs, 'msecs');
if (callback) this.once('timeout', callback);

if (this.socket) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/stream_base_commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function setStreamTimeout(msecs, callback) {
this.timeout = msecs;

// Type checking identical to timers.enroll()
msecs = validateTimerDuration(msecs);
msecs = validateTimerDuration(msecs, 'msecs');

// Attempt to clear an existing timer in both cases -
// even if it will be rescheduled we don't want to leak an existing timer.
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ function setUnrefTimeout(callback, after, arg1, arg2, arg3) {
}

// Type checking used by timers.enroll() and Socket#setTimeout()
function validateTimerDuration(msecs) {
validateNumber(msecs, 'msecs');
function validateTimerDuration(msecs, name) {
validateNumber(msecs, name);
if (msecs < 0 || !isFinite(msecs)) {
throw new ERR_OUT_OF_RANGE('msecs', 'a non-negative finite number', msecs);
throw new ERR_OUT_OF_RANGE(name, 'a non-negative finite number', msecs);
}

// Ensure that msecs fits into signed int32
Expand Down
2 changes: 1 addition & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ exports.unenroll = util.deprecate(unenroll,
// This function does not start the timer, see `active()`.
// Using existing objects as timers slightly reduces object overhead.
function enroll(item, msecs) {
msecs = validateTimerDuration(msecs);
msecs = validateTimerDuration(msecs, 'msecs');

// if this item was already in a list somewhere
// then we should unenroll it from that
Expand Down

0 comments on commit a1d05e4

Please sign in to comment.