From 89113b8cfe3c82115dde21c9eedcde4a493aa0cb Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Thu, 8 Dec 2022 00:42:53 +0900 Subject: [PATCH 1/2] lib: refactor to use min/max of `validateNumber` Fix NumberIsNaN is called two times and change error code to ERR_OUT_OF_RANGE when RangeError is occurred in test. --- lib/internal/histogram.js | 11 ++--------- test/sequential/test-performance-eventloopdelay.js | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index ca540e555e0160..782e1970d53b03 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -2,7 +2,6 @@ const { MapPrototypeEntries, - NumberIsNaN, NumberIsInteger, NumberMAX_SAFE_INTEGER, ObjectFromEntries, @@ -186,10 +185,7 @@ class Histogram { percentile(percentile) { 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); + validateNumber(percentile, 'percentile', 1, 100); return this[kHandle]?.percentile(percentile); } @@ -201,10 +197,7 @@ class Histogram { percentileBigInt(percentile) { 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); + validateNumber(percentile, 'percentile', 1, 100); return this[kHandle]?.percentileBigInt(percentile); } diff --git a/test/sequential/test-performance-eventloopdelay.js b/test/sequential/test-performance-eventloopdelay.js index f262e9de3fe6cb..24e5744118cc38 100644 --- a/test/sequential/test-performance-eventloopdelay.js +++ b/test/sequential/test-performance-eventloopdelay.js @@ -91,7 +91,7 @@ const { sleep } = require('internal/util'); () => histogram.percentile(i), { name: 'RangeError', - code: 'ERR_INVALID_ARG_VALUE' + code: 'ERR_OUT_OF_RANGE' } ); }); From 1830cafab1c46a5f336981ae22f68ff02c25fc83 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Thu, 8 Dec 2022 08:27:49 +0900 Subject: [PATCH 2/2] lib: refactor to use `validateInteger` Use validateInteger for record and revise error code of percentile. --- lib/internal/histogram.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index 782e1970d53b03..23b8b8a7652dc4 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -2,7 +2,7 @@ const { MapPrototypeEntries, - NumberIsInteger, + NumberIsNaN, NumberMAX_SAFE_INTEGER, ObjectFromEntries, ReflectConstruct, @@ -185,7 +185,9 @@ class Histogram { percentile(percentile) { if (!isHistogram(this)) throw new ERR_INVALID_THIS('Histogram'); - validateNumber(percentile, 'percentile', 1, 100); + validateNumber(percentile, 'percentile'); + if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100) + throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile); return this[kHandle]?.percentile(percentile); } @@ -197,7 +199,9 @@ class Histogram { percentileBigInt(percentile) { if (!isHistogram(this)) throw new ERR_INVALID_THIS('Histogram'); - validateNumber(percentile, 'percentile', 1, 100); + validateNumber(percentile, 'percentile'); + if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100) + throw new ERR_OUT_OF_RANGE('percentile', '> 0 && <= 100', percentile); return this[kHandle]?.percentileBigInt(percentile); } @@ -277,11 +281,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); }