Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experiment] Clear Error stack from extra entries #996

Merged
merged 2 commits into from Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/core-js/internals/clear-error-stack.js
@@ -0,0 +1,20 @@
var uncurryThis = require('../internals/function-uncurry-this');
var arraySlice = require('../internals/array-slice');

var replace = uncurryThis(''.replace);
var split = uncurryThis(''.split);
var join = uncurryThis([].join);

var TEST = (function (arg) { return String(Error(arg).stack); })('zxcasd');
var V8_OR_CHAKRA_STACK_ENTRY = /\n\s*at [^:]*:[^\n]*/;
var IS_V8_OR_CHAKRA_STACK = V8_OR_CHAKRA_STACK_ENTRY.test(TEST);
var IS_FIREFOX_OR_SAFARI_STACK = /@[^\n]*\n/.test(TEST) && !/zxcasd/.test(TEST);

module.exports = function (stack, dropEntries) {
if (typeof stack != 'string') return stack;
if (IS_V8_OR_CHAKRA_STACK) {
while (dropEntries--) stack = replace(stack, V8_OR_CHAKRA_STACK_ENTRY, '');
} else if (IS_FIREFOX_OR_SAFARI_STACK) {
return join(arraySlice(split(stack, '\n'), dropEntries), '\n');
} return stack;
};
10 changes: 10 additions & 0 deletions packages/core-js/internals/error-stack-installable.js
@@ -0,0 +1,10 @@
var fails = require('../internals/fails');
var createPropertyDescriptor = require('../internals/create-property-descriptor');

module.exports = !fails(function () {
var error = Error('a');
if (!('stack' in error)) return true;
// eslint-disable-next-line es/no-object-defineproperty -- safe
Object.defineProperty(error, 'stack', createPropertyDescriptor(1, 7));
return error.stack !== 7;
});
8 changes: 5 additions & 3 deletions packages/core-js/modules/es.aggregate-error.js
Expand Up @@ -6,21 +6,23 @@ var copyConstructorProperties = require('../internals/copy-constructor-propertie
var create = require('../internals/object-create');
var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
var createPropertyDescriptor = require('../internals/create-property-descriptor');
var clearErrorStack = require('../internals/clear-error-stack');
var installErrorCause = require('../internals/install-error-cause');
var iterate = require('../internals/iterate');
var normalizeStringArgument = require('../internals/normalize-string-argument');
var ERROR_STACK_INSTALLABLE = require('../internals/error-stack-installable');

var push = [].push;

var $AggregateError = function AggregateError(errors, message /* , options */) {
var that = this;
var that = this instanceof $AggregateError ? this : create($AggregateErrorPrototype);
var options = arguments.length > 2 ? arguments[2] : undefined;
if (!(that instanceof $AggregateError)) return new $AggregateError(errors, message, options);
if (setPrototypeOf) {
// eslint-disable-next-line unicorn/error-message -- expected
that = setPrototypeOf(new Error(undefined), getPrototypeOf(that));
}
createNonEnumerableProperty(that, 'message', normalizeStringArgument(message, ''));
if (ERROR_STACK_INSTALLABLE) createNonEnumerableProperty(that, 'stack', clearErrorStack(that.stack, 1));
installErrorCause(that, options);
var errorsArray = [];
iterate(errors, push, { that: errorsArray });
Expand All @@ -31,7 +33,7 @@ var $AggregateError = function AggregateError(errors, message /* , options */) {
if (setPrototypeOf) setPrototypeOf($AggregateError, Error);
else copyConstructorProperties($AggregateError, Error);

$AggregateError.prototype = create(Error.prototype, {
var $AggregateErrorPrototype = $AggregateError.prototype = create(Error.prototype, {
constructor: createPropertyDescriptor(1, $AggregateError),
message: createPropertyDescriptor(1, ''),
name: createPropertyDescriptor(1, 'AggregateError')
Expand Down