summaryrefslogtreecommitdiff
path: root/lib/buffer.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/buffer.js
parent1ed3c54ecbd72a33693e5954f86bcc9fd9b1cc09 (diff)
downloadnode-new-bfbce289c33b12aafb82bd5b45bcb4412850b28f.tar.gz
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/buffer.js')
-rw-r--r--lib/buffer.js37
1 files changed, 17 insertions, 20 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index c5497e18aa..b072ac407d 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -62,15 +62,18 @@ const {
} = require('internal/util/inspect');
const {
- ERR_BUFFER_OUT_OF_BOUNDS,
- ERR_OUT_OF_RANGE,
- ERR_INVALID_ARG_TYPE,
- ERR_INVALID_ARG_VALUE,
- ERR_INVALID_BUFFER_SIZE,
- ERR_INVALID_OPT_VALUE,
- ERR_NO_LONGER_SUPPORTED,
- ERR_UNKNOWN_ENCODING
-} = require('internal/errors').codes;
+ codes: {
+ ERR_BUFFER_OUT_OF_BOUNDS,
+ ERR_OUT_OF_RANGE,
+ ERR_INVALID_ARG_TYPE,
+ ERR_INVALID_ARG_VALUE,
+ ERR_INVALID_BUFFER_SIZE,
+ ERR_INVALID_OPT_VALUE,
+ ERR_NO_LONGER_SUPPORTED,
+ ERR_UNKNOWN_ENCODING
+ },
+ hideStackFrames
+} = require('internal/errors');
const { validateString } = require('internal/validators');
const {
@@ -247,20 +250,14 @@ Object.setPrototypeOf(Buffer, Uint8Array);
// The 'assertSize' method will remove itself from the callstack when an error
// occurs. This is done simply to keep the internal details of the
// implementation from bleeding out to users.
-function assertSize(size) {
- let err = null;
-
+const assertSize = hideStackFrames((size) => {
if (typeof size !== 'number') {
- err = new ERR_INVALID_ARG_TYPE('size', 'number', size);
- } else if (!(size >= 0 && size <= kMaxLength)) {
- err = new ERR_INVALID_OPT_VALUE.RangeError('size', size);
+ throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
}
-
- if (err !== null) {
- Error.captureStackTrace(err, assertSize);
- throw err;
+ if (!(size >= 0 && size <= kMaxLength)) {
+ throw new ERR_INVALID_OPT_VALUE.RangeError('size', size);
}
-}
+});
/**
* Creates a new filled Buffer instance.