Skip to content

Commit

Permalink
Merge pull request #996 from zloirock/error-stack-experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock authored Oct 21, 2021
2 parents 7b37973 + a4a4783 commit b1287f6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/core-js/internals/clear-error-stack.js
Original file line number Diff line number Diff line change
@@ -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
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 @@ -7,21 +7,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 Error = global.Error;
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) {
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 @@ -32,7 +34,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 b1287f6

Please sign in to comment.