Skip to content

Commit

Permalink
implement Error cloning
Browse files Browse the repository at this point in the history
I'm not 100% sure stack cloning would work everywhere, but it seems to
work on Firefox and Chrome and Node.js.
  • Loading branch information
BasixKOR committed Sep 15, 2021
1 parent 1b01d70 commit 73b5186
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/core-js/internals/structured-clone.js
@@ -1,7 +1,5 @@
/* eslint-disable es/no-map -- safe */
/* eslint-disable es/no-set -- safe */
/* eslint-disable no-new-wrappers -- safe */
/* eslint-disable es/no-bigint -- safe */
'use const';
var isSymbol = require('./is-symbol');
var toObject = require('./to-object');
Expand Down Expand Up @@ -50,6 +48,16 @@ module.exports = function structuredCloneInternal(weakmap, value) {
cloned = new Set();
deep = true;
break;
case 'Error':
case 'EvalError':
case 'RangeError':
case 'ReferenceError':
case 'SyntaxError':
case 'TypeError':
case 'URIError':
cloned = value.constructor(value.message.toString());
deep = true; // clone stack after storing in the weakmap
break;
case 'Array':
cloned = [];
deep = true;
Expand All @@ -75,6 +83,16 @@ module.exports = function structuredCloneInternal(weakmap, value) {
cloned.add(structuredCloneInternal(weakmap, v));
});
break;
case 'Error':
// Attempt to clone the stack.
if (!Object.prototype.hasOwnProperty.call(Error.prototype, 'stack')) break;
try {
cloned.stack = structuredCloneInternal(weakmap, value.stack);
} catch (error) {
if (classof(error) === 'TypeError') return cloned; // Stack cloning not avaliable.
throw error; // Unexpected error while cloning.
}
break;
case 'Array':
case 'Object':
var properties = getOwnPropertyNames.f(value);
Expand Down

0 comments on commit 73b5186

Please sign in to comment.