123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- 'use strict';
- Zone.__load_patch('Error', (global, Zone, api) => {
-
- const zoneJsInternalStackFramesSymbol = api.symbol('zoneJsInternalStackFrames');
- const NativeError = global[api.symbol('Error')] = global['Error'];
-
- const zoneJsInternalStackFrames = {};
-
- let zoneAwareFrame1;
- let zoneAwareFrame2;
- let zoneAwareFrame1WithoutNew;
- let zoneAwareFrame2WithoutNew;
- let zoneAwareFrame3WithoutNew;
- global['Error'] = ZoneAwareError;
- const stackRewrite = 'stackRewrite';
- const zoneJsInternalStackFramesPolicy = global['__Zone_Error_BlacklistedStackFrames_policy'] ||
- global['__Zone_Error_ZoneJsInternalStackFrames_policy'] || 'default';
- function buildZoneFrameNames(zoneFrame) {
- let zoneFrameName = { zoneName: zoneFrame.zone.name };
- let result = zoneFrameName;
- while (zoneFrame.parent) {
- zoneFrame = zoneFrame.parent;
- const parentZoneFrameName = { zoneName: zoneFrame.zone.name };
- zoneFrameName.parent = parentZoneFrameName;
- zoneFrameName = parentZoneFrameName;
- }
- return result;
- }
- function buildZoneAwareStackFrames(originalStack, zoneFrame, isZoneFrame = true) {
- let frames = originalStack.split('\n');
- let i = 0;
-
- while (!(frames[i] === zoneAwareFrame1 || frames[i] === zoneAwareFrame2 ||
- frames[i] === zoneAwareFrame1WithoutNew || frames[i] === zoneAwareFrame2WithoutNew ||
- frames[i] === zoneAwareFrame3WithoutNew) &&
- i < frames.length) {
- i++;
- }
- for (; i < frames.length && zoneFrame; i++) {
- let frame = frames[i];
- if (frame.trim()) {
- switch (zoneJsInternalStackFrames[frame]) {
- case 0 :
- frames.splice(i, 1);
- i--;
- break;
- case 1 :
- if (zoneFrame.parent) {
-
- zoneFrame = zoneFrame.parent;
- }
- else {
- zoneFrame = null;
- }
- frames.splice(i, 1);
- i--;
- break;
- default:
- frames[i] += isZoneFrame ? ` [${zoneFrame.zone.name}]` :
- ` [${zoneFrame.zoneName}]`;
- }
- }
- }
- return frames.join('\n');
- }
-
- function ZoneAwareError() {
-
- let error = NativeError.apply(this, arguments);
-
- const originalStack = error['originalStack'] = error.stack;
-
- if (ZoneAwareError[stackRewrite] && originalStack) {
- let zoneFrame = api.currentZoneFrame();
- if (zoneJsInternalStackFramesPolicy === 'lazy') {
-
- error[api.symbol('zoneFrameNames')] = buildZoneFrameNames(zoneFrame);
- }
- else if (zoneJsInternalStackFramesPolicy === 'default') {
- try {
- error.stack = error.zoneAwareStack = buildZoneAwareStackFrames(originalStack, zoneFrame);
- }
- catch (e) {
-
- }
- }
- }
- if (this instanceof NativeError && this.constructor != NativeError) {
-
-
- Object.keys(error).concat('stack', 'message').forEach((key) => {
- const value = error[key];
- if (value !== undefined) {
- try {
- this[key] = value;
- }
- catch (e) {
-
- }
- }
- });
- return this;
- }
- return error;
- }
-
- ZoneAwareError.prototype = NativeError.prototype;
- ZoneAwareError[zoneJsInternalStackFramesSymbol] = zoneJsInternalStackFrames;
- ZoneAwareError[stackRewrite] = false;
- const zoneAwareStackSymbol = api.symbol('zoneAwareStack');
-
- if (zoneJsInternalStackFramesPolicy === 'lazy') {
- Object.defineProperty(ZoneAwareError.prototype, 'zoneAwareStack', {
- configurable: true,
- enumerable: true,
- get: function () {
- if (!this[zoneAwareStackSymbol]) {
- this[zoneAwareStackSymbol] = buildZoneAwareStackFrames(this.originalStack, this[api.symbol('zoneFrameNames')], false);
- }
- return this[zoneAwareStackSymbol];
- },
- set: function (newStack) {
- this.originalStack = newStack;
- this[zoneAwareStackSymbol] = buildZoneAwareStackFrames(this.originalStack, this[api.symbol('zoneFrameNames')], false);
- }
- });
- }
-
- const specialPropertyNames = ['stackTraceLimit', 'captureStackTrace', 'prepareStackTrace'];
-
- const nativeErrorProperties = Object.keys(NativeError);
- if (nativeErrorProperties) {
- nativeErrorProperties.forEach(prop => {
- if (specialPropertyNames.filter(sp => sp === prop).length === 0) {
- Object.defineProperty(ZoneAwareError, prop, {
- get: function () {
- return NativeError[prop];
- },
- set: function (value) {
- NativeError[prop] = value;
- }
- });
- }
- });
- }
- if (NativeError.hasOwnProperty('stackTraceLimit')) {
-
- NativeError.stackTraceLimit = Math.max(NativeError.stackTraceLimit, 15);
-
- Object.defineProperty(ZoneAwareError, 'stackTraceLimit', {
- get: function () {
- return NativeError.stackTraceLimit;
- },
- set: function (value) {
- return NativeError.stackTraceLimit = value;
- }
- });
- }
- if (NativeError.hasOwnProperty('captureStackTrace')) {
- Object.defineProperty(ZoneAwareError, 'captureStackTrace', {
-
-
- value: function zoneCaptureStackTrace(targetObject, constructorOpt) {
- NativeError.captureStackTrace(targetObject, constructorOpt);
- }
- });
- }
- const ZONE_CAPTURESTACKTRACE = 'zoneCaptureStackTrace';
- Object.defineProperty(ZoneAwareError, 'prepareStackTrace', {
- get: function () {
- return NativeError.prepareStackTrace;
- },
- set: function (value) {
- if (!value || typeof value !== 'function') {
- return NativeError.prepareStackTrace = value;
- }
- return NativeError.prepareStackTrace = function (error, structuredStackTrace) {
-
- if (structuredStackTrace) {
- for (let i = 0; i < structuredStackTrace.length; i++) {
- const st = structuredStackTrace[i];
-
- if (st.getFunctionName() === ZONE_CAPTURESTACKTRACE) {
- structuredStackTrace.splice(i, 1);
- break;
- }
- }
- }
- return value.call(this, error, structuredStackTrace);
- };
- }
- });
- if (zoneJsInternalStackFramesPolicy === 'disable') {
-
- return;
- }
-
-
-
-
- let detectZone = Zone.current.fork({
- name: 'detect',
- onHandleError: function (parentZD, current, target, error) {
- if (error.originalStack && Error === ZoneAwareError) {
- let frames = error.originalStack.split(/\n/);
- let runFrame = false, runGuardedFrame = false, runTaskFrame = false;
- while (frames.length) {
- let frame = frames.shift();
-
-
-
- if (/:\d+:\d+/.test(frame) || frame === 'ZoneAwareError') {
-
-
-
-
-
-
- let fnName = frame.split('(')[0].split('@')[0];
- let frameType = 1 ;
- if (fnName.indexOf('ZoneAwareError') !== -1) {
- if (fnName.indexOf('new ZoneAwareError') !== -1) {
- zoneAwareFrame1 = frame;
- zoneAwareFrame2 = frame.replace('new ZoneAwareError', 'new Error.ZoneAwareError');
- }
- else {
- zoneAwareFrame1WithoutNew = frame;
- zoneAwareFrame2WithoutNew = frame.replace('Error.', '');
- if (frame.indexOf('Error.ZoneAwareError') === -1) {
- zoneAwareFrame3WithoutNew =
- frame.replace('ZoneAwareError', 'Error.ZoneAwareError');
- }
- }
- zoneJsInternalStackFrames[zoneAwareFrame2] = 0 ;
- }
- if (fnName.indexOf('runGuarded') !== -1) {
- runGuardedFrame = true;
- }
- else if (fnName.indexOf('runTask') !== -1) {
- runTaskFrame = true;
- }
- else if (fnName.indexOf('run') !== -1) {
- runFrame = true;
- }
- else {
- frameType = 0 ;
- }
- zoneJsInternalStackFrames[frame] = frameType;
-
- if (runFrame && runGuardedFrame && runTaskFrame) {
- ZoneAwareError[stackRewrite] = true;
- break;
- }
- }
- }
- }
- return false;
- }
- });
-
-
- const childDetectZone = detectZone.fork({
- name: 'child',
- onScheduleTask: function (delegate, curr, target, task) {
- return delegate.scheduleTask(target, task);
- },
- onInvokeTask: function (delegate, curr, target, task, applyThis, applyArgs) {
- return delegate.invokeTask(target, task, applyThis, applyArgs);
- },
- onCancelTask: function (delegate, curr, target, task) {
- return delegate.cancelTask(target, task);
- },
- onInvoke: function (delegate, curr, target, callback, applyThis, applyArgs, source) {
- return delegate.invoke(target, callback, applyThis, applyArgs, source);
- }
- });
-
-
-
-
- const originalStackTraceLimit = Error.stackTraceLimit;
- Error.stackTraceLimit = 100;
-
-
-
- childDetectZone.run(() => {
- childDetectZone.runGuarded(() => {
- const fakeTransitionTo = () => { };
- childDetectZone.scheduleEventTask(zoneJsInternalStackFramesSymbol, () => {
- childDetectZone.scheduleMacroTask(zoneJsInternalStackFramesSymbol, () => {
- childDetectZone.scheduleMicroTask(zoneJsInternalStackFramesSymbol, () => {
- throw new Error();
- }, undefined, (t) => {
- t._transitionTo = fakeTransitionTo;
- t.invoke();
- });
- childDetectZone.scheduleMicroTask(zoneJsInternalStackFramesSymbol, () => {
- throw Error();
- }, undefined, (t) => {
- t._transitionTo = fakeTransitionTo;
- t.invoke();
- });
- }, undefined, (t) => {
- t._transitionTo = fakeTransitionTo;
- t.invoke();
- }, () => { });
- }, undefined, (t) => {
- t._transitionTo = fakeTransitionTo;
- t.invoke();
- }, () => { });
- });
- });
- Error.stackTraceLimit = originalStackTraceLimit;
- });
|