Skip to content

Commit

Permalink
lib: refactor to use min/max of validateNumber
Browse files Browse the repository at this point in the history
Fix NumberIsNaN is called two times and change error code
to ERR_OUT_OF_RANGE when RangeError is occurred in test.

PR-URL: #45772
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
  • Loading branch information
deokjinkim committed Feb 3, 2023
1 parent bfadee5 commit 88d71dc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
13 changes: 3 additions & 10 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const {
MapPrototypeEntries,
NumberIsNaN,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
ObjectFromEntries,
ReflectConstruct,
Expand Down Expand Up @@ -186,9 +185,8 @@ class Histogram {
if (!isHistogram(this))
throw new ERR_INVALID_THIS('Histogram');
validateNumber(percentile, 'percentile');

if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile);

return this[kHandle]?.percentile(percentile);
}
Expand All @@ -201,9 +199,8 @@ class Histogram {
if (!isHistogram(this))
throw new ERR_INVALID_THIS('Histogram');
validateNumber(percentile, 'percentile');

if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile);

return this[kHandle]?.percentileBigInt(percentile);
}
Expand Down Expand Up @@ -283,11 +280,7 @@ class RecordableHistogram extends Histogram {
return;
}

if (!NumberIsInteger(val))
throw new ERR_INVALID_ARG_TYPE('val', ['integer', 'bigint'], val);

if (val < 1 || val > NumberMAX_SAFE_INTEGER)
throw new ERR_OUT_OF_RANGE('val', 'a safe integer greater than 0', val);
validateInteger(val, 'val', 1);

this[kHandle]?.record(val);
}
Expand Down
2 changes: 1 addition & 1 deletion test/sequential/test-performance-eventloopdelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const { sleep } = require('internal/util');
() => histogram.percentile(i),
{
name: 'RangeError',
code: 'ERR_INVALID_ARG_VALUE'
code: 'ERR_OUT_OF_RANGE'
}
);
});
Expand Down

0 comments on commit 88d71dc

Please sign in to comment.