Skip to content

Commit

Permalink
[experiment] clear error stack from extra entries
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 17, 2021
1 parent 9f8af38 commit e45e01a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/core-js/internals/clear-error-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var replace = ''.replace;
var split = ''.split;
var slice = [].slice;
var join = [].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.call(stack, V8_OR_CHAKRA_STACK_ENTRY, '');
} else if (IS_FIREFOX_OR_SAFARI_STACK) {
return join.call(slice.call(split.call(stack, '\n'), dropEntries), '\n');
} return stack;
};
10 changes: 10 additions & 0 deletions packages/core-js/internals/error-stack-installable.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ 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 $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, errorsArray.push, { that: errorsArray });
Expand All @@ -29,7 +31,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

0 comments on commit e45e01a

Please sign in to comment.