|
| 1 | +/** |
| 2 | +* @license |
| 3 | +* Copyright Google Inc. All Rights Reserved. |
| 4 | +* |
| 5 | +* Use of this source code is governed by an MIT-style license that can be |
| 6 | +* found in the LICENSE file at https://angular.io/license |
| 7 | +*/ |
| 8 | +(function (global, factory) { |
| 9 | + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
| 10 | + typeof define === 'function' && define.amd ? define(factory) : |
| 11 | + (factory()); |
| 12 | +}(this, (function () { 'use strict'; |
| 13 | + |
| 14 | +/** |
| 15 | + * @license |
| 16 | + * Copyright Google Inc. All Rights Reserved. |
| 17 | + * |
| 18 | + * Use of this source code is governed by an MIT-style license that can be |
| 19 | + * found in the LICENSE file at https://angular.io/license |
| 20 | + */ |
| 21 | +((function (_global) { |
| 22 | + var ErrorType = _global['Error']; |
| 23 | + var blackListedStackFrames = {}; |
| 24 | + var zoneAwareFunctionNames = ['Zone', 'drainMicrotask', 'getStacktraceWithUncaughtError', 'new LongStackTrace', 'Object.onScheduleTask']; |
| 25 | + var detectZone = Zone.root.fork(Zone['longStackTraceZoneSpec']).fork({ |
| 26 | + name: 'detectZone', |
| 27 | + onHandleError: function (parentDelegate, currentZone, targetZone, error) { |
| 28 | + parentDelegate.handleError(targetZone, error); |
| 29 | + var frames = error.stack.split(/\n/); |
| 30 | + var runFrame = false, runGuardedFrame = false, runTaskFrame = false; |
| 31 | + var _loop_1 = function () { |
| 32 | + var frame = frames.shift(); |
| 33 | + // On safari it is possible to have stack frame with no line number. |
| 34 | + // This check makes sure that we don't filter frames on name only (must have |
| 35 | + // linenumber) |
| 36 | + if (/:\d+:\d+/.test(frame)) { |
| 37 | + var f_1 = frame.split(' [')[0]; |
| 38 | + if (zoneAwareFunctionNames.filter(function (zf) { return f_1.toLowerCase().indexOf(f_1.toLowerCase()) !== -1; }).length > 0) { |
| 39 | + blackListedStackFrames[f_1] = f_1; |
| 40 | + } |
| 41 | + } |
| 42 | + }; |
| 43 | + while (frames.length) { |
| 44 | + _loop_1(); |
| 45 | + } |
| 46 | + return false; |
| 47 | + } |
| 48 | + }); |
| 49 | + var detectZoneWithCallbacks = Zone.root.fork(Zone['longStackTraceZoneSpec']).fork({ |
| 50 | + name: 'detectZone', |
| 51 | + onFork: function (parentDelegate, currentZone, targetZone, zoneSpec) { |
| 52 | + return parentDelegate.fork(targetZone, zoneSpec); |
| 53 | + }, |
| 54 | + onIntercept: function (parentDelegate, currentZone, targetZone, delegate, source) { |
| 55 | + return parentDelegate.intercept(targetZone, delegate, source); |
| 56 | + }, |
| 57 | + onInvoke: function (parentZoneDelegate, currentZone, targetZone, delegate, applyThis, applyArgs, source) { |
| 58 | + return parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source); |
| 59 | + }, |
| 60 | + onScheduleTask: function (parentZoneDelegate, currentZone, targetZone, task) { |
| 61 | + return parentZoneDelegate.scheduleTask(targetZone, task); |
| 62 | + }, |
| 63 | + onInvokeTask: function (parentZoneDelegate, currentZone, targetZone, task, applyThis, applyArgs) { |
| 64 | + return parentZoneDelegate.invokeTask(targetZone, task, applyThis, applyArgs); |
| 65 | + }, |
| 66 | + onCancelTask: function (parentZoneDelegate, currentZone, targetZone, task) { |
| 67 | + return parentZoneDelegate.cancelTask(targetZone, task); |
| 68 | + }, |
| 69 | + onHasTask: function (delegate, current, target, hasTaskState) { |
| 70 | + return delegate.hasTask(target, hasTaskState); |
| 71 | + }, |
| 72 | + onHandleError: function (parentDelegate, currentZone, targetZone, error) { |
| 73 | + parentDelegate.handleError(targetZone, error); |
| 74 | + var frames = error.stack.split(/\n/); |
| 75 | + var runFrame = false, runGuardedFrame = false, runTaskFrame = false; |
| 76 | + while (frames.length) { |
| 77 | + var frame = frames.shift(); |
| 78 | + // On safari it is possible to have stack frame with no line number. |
| 79 | + // This check makes sure that we don't filter frames on name only (must have |
| 80 | + // linenumber) |
| 81 | + if (/:\d+:\d+/.test(frame)) { |
| 82 | + var f = frame.split(' [')[0]; |
| 83 | + if (f.indexOf('Zone') !== -1 || f.toLowerCase().indexOf('zoneaware') !== -1 || f.indexOf('drainMicroTaskQueue') !== -1) { |
| 84 | + blackListedStackFrames[f] = f; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return false; |
| 89 | + } |
| 90 | + }); |
| 91 | + var detectFn = function () { |
| 92 | + throw new Error('zoneAwareFrames'); |
| 93 | + }; |
| 94 | + var detectPromiseFn = function () { |
| 95 | + var p = new Promise(function (resolve, reject) { |
| 96 | + reject(new Error('zoneAwareFrames')); |
| 97 | + }); |
| 98 | + }; |
| 99 | + var detectPromiseCaughtFn = function () { |
| 100 | + var p = new Promise(function (resolve, reject) { |
| 101 | + reject(new Error('zoneAwareFrames')); |
| 102 | + }); |
| 103 | + p.catch(function (err) { |
| 104 | + throw err; |
| 105 | + }); |
| 106 | + }; |
| 107 | + // Cause the error to extract the stack frames. |
| 108 | + detectZone.runTask(detectZone.scheduleEventTask('detect', detectFn, null, function () { return null; }, null)); |
| 109 | + detectZoneWithCallbacks.runTask(detectZoneWithCallbacks.scheduleEventTask('detect', detectFn, null, function () { return null; }, null)); |
| 110 | + detectZone.runTask(detectZone.scheduleMacroTask('detect', detectFn, null, function () { return null; }, null)); |
| 111 | + detectZoneWithCallbacks.runTask(detectZoneWithCallbacks.scheduleMacroTask('detect', detectFn, null, function () { return null; }, null)); |
| 112 | + detectZone.runTask(detectZone.scheduleMicroTask('detect', detectFn, null, function () { return null; })); |
| 113 | + detectZoneWithCallbacks.runTask(detectZoneWithCallbacks.scheduleMicroTask('detect', detectFn, null, function () { return null; })); |
| 114 | + detectZone.runGuarded(function () { |
| 115 | + detectZone.run(detectFn); |
| 116 | + }); |
| 117 | + detectZoneWithCallbacks.runGuarded(function () { |
| 118 | + detectZone.run(detectFn); |
| 119 | + }); |
| 120 | + detectZone.runGuarded(detectPromiseFn); |
| 121 | + detectZoneWithCallbacks.runGuarded(detectPromiseFn); |
| 122 | + detectZone.runGuarded(detectPromiseCaughtFn); |
| 123 | + detectZoneWithCallbacks.runGuarded(detectPromiseCaughtFn); |
| 124 | + var otherZoneAwareFunctionNames = ['ZoneTask.invoke', 'ZoneAware']; |
| 125 | + Object.defineProperty(ErrorType, 'getNonZoneAwareStack', { |
| 126 | + value: function (err) { |
| 127 | + if (err.stack) { |
| 128 | + var frames_1 = err.stack.split('\n'); |
| 129 | + var simplifiedFrames = []; |
| 130 | + var _loop_2 = function (i) { |
| 131 | + var frame = frames_1[i].split(' [')[0]; |
| 132 | + var frameWithoutZone = frame.split(' [')[0]; |
| 133 | + if (blackListedStackFrames.hasOwnProperty(frameWithoutZone) && |
| 134 | + blackListedStackFrames[frameWithoutZone]) { |
| 135 | + frames_1.splice(i, 1); |
| 136 | + i--; |
| 137 | + } |
| 138 | + else if (otherZoneAwareFunctionNames.filter(function (f) { return frame.toLowerCase().indexOf(f.toLowerCase()) !== -1; }).length > 0) { |
| 139 | + frames_1.splice(i, 1); |
| 140 | + i--; |
| 141 | + } |
| 142 | + else { |
| 143 | + simplifiedFrames.push(frame); |
| 144 | + } |
| 145 | + out_i_1 = i; |
| 146 | + }; |
| 147 | + var out_i_1; |
| 148 | + for (var i = 0; i < frames_1.length; i++) { |
| 149 | + _loop_2(i); |
| 150 | + i = out_i_1; |
| 151 | + } |
| 152 | + return simplifiedFrames.join('\n'); |
| 153 | + } |
| 154 | + return err.stack; |
| 155 | + } |
| 156 | + }); |
| 157 | +})(typeof window === 'object' && window || typeof self === 'object' && self || global)); |
| 158 | + |
| 159 | +}))); |
0 commit comments