summaryrefslogtreecommitdiff
path: root/lib/internal/validators.js
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2019-03-18 02:29:39 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2019-03-23 02:55:54 +0100
commitbfbce289c33b12aafb82bd5b45bcb4412850b28f (patch)
treefa9c21007fbe096b11e5a10d80de4b0af91137e4 /lib/internal/validators.js
parent1ed3c54ecbd72a33693e5954f86bcc9fd9b1cc09 (diff)
downloadandroid-node-v8-bfbce289c33b12aafb82bd5b45bcb4412850b28f.tar.gz
android-node-v8-bfbce289c33b12aafb82bd5b45bcb4412850b28f.tar.bz2
android-node-v8-bfbce289c33b12aafb82bd5b45bcb4412850b28f.zip
lib: refactor Error.captureStackTrace() usage
When using `Errors.captureStackFrames` the error's stack property is set again. This adds a helper function that wraps this functionality in a simple API that does not only set the stack including the `code` property but it also improves the performance to create the error. The helper works for thrown errors and errors returned from wrapped functions in case they are Node.js core errors. PR-URL: https://github.com/nodejs/node/pull/26738 Fixes: https://github.com/nodejs/node/issues/26669 Fixes: https://github.com/nodejs/node/issues/20253 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaƫl Zasso <targos@protonmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'lib/internal/validators.js')
-rw-r--r--lib/internal/validators.js97
1 files changed, 43 insertions, 54 deletions
diff --git a/lib/internal/validators.js b/lib/internal/validators.js
index 6de46349c0..a80917ee7e 100644
--- a/lib/internal/validators.js
+++ b/lib/internal/validators.js
@@ -1,10 +1,13 @@
'use strict';
const {
- ERR_INVALID_ARG_TYPE,
- ERR_INVALID_ARG_VALUE,
- ERR_OUT_OF_RANGE
-} = require('internal/errors').codes;
+ hideStackFrames,
+ codes: {
+ ERR_INVALID_ARG_TYPE,
+ ERR_INVALID_ARG_VALUE,
+ ERR_OUT_OF_RANGE
+ }
+} = require('internal/errors');
function isInt32(value) {
return value === (value | 0);
@@ -52,66 +55,52 @@ function validateMode(value, name, def) {
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
}
-function validateInteger(value, name) {
- let err;
-
+const validateInteger = hideStackFrames((value, name) => {
if (typeof value !== 'number')
- err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
- else if (!Number.isSafeInteger(value))
- err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
-
- if (err) {
- Error.captureStackTrace(err, validateInteger);
- throw err;
- }
-
+ throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
+ if (!Number.isSafeInteger(value))
+ throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
return value;
-}
-
-function validateInt32(value, name, min = -2147483648, max = 2147483647) {
- // The defaults for min and max correspond to the limits of 32-bit integers.
- if (!isInt32(value)) {
- let err;
- if (typeof value !== 'number') {
- err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
- } else if (!Number.isInteger(value)) {
- err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
- } else {
- err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
+});
+
+const validateInt32 = hideStackFrames(
+ (value, name, min = -2147483648, max = 2147483647) => {
+ // The defaults for min and max correspond to the limits of 32-bit integers.
+ if (!isInt32(value)) {
+ if (typeof value !== 'number') {
+ throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
+ }
+ if (!Number.isInteger(value)) {
+ throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
+ }
+ throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
}
- Error.captureStackTrace(err, validateInt32);
- throw err;
- } else if (value < min || value > max) {
- const err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
- Error.captureStackTrace(err, validateInt32);
- throw err;
+ if (value < min || value > max) {
+ throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
+ }
+ return value;
}
+);
- return value;
-}
-
-function validateUint32(value, name, positive) {
+const validateUint32 = hideStackFrames((value, name, positive) => {
if (!isUint32(value)) {
- let err;
if (typeof value !== 'number') {
- err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
- } else if (!Number.isInteger(value)) {
- err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
- } else {
- const min = positive ? 1 : 0;
- // 2 ** 32 === 4294967296
- err = new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
+ throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
- Error.captureStackTrace(err, validateUint32);
- throw err;
- } else if (positive && value === 0) {
- const err = new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
- Error.captureStackTrace(err, validateUint32);
- throw err;
+ if (!Number.isInteger(value)) {
+ throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
+ }
+ const min = positive ? 1 : 0;
+ // 2 ** 32 === 4294967296
+ throw new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
}
-
+ if (positive && value === 0) {
+ throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
+ }
+ // TODO(BridgeAR): Remove return values from validation functions and
+ // especially reduce side effects caused by validation functions.
return value;
-}
+});
function validateString(value, name) {
if (typeof value !== 'string')