[6a3a178] | 1 | 'use strict';
|
---|
| 2 | /**
|
---|
| 3 | * @license Angular v12.0.0-next.0
|
---|
| 4 | * (c) 2010-2020 Google LLC. https://angular.io/
|
---|
| 5 | * License: MIT
|
---|
| 6 | */
|
---|
| 7 | /**
|
---|
| 8 | * @license
|
---|
| 9 | * Copyright Google LLC All Rights Reserved.
|
---|
| 10 | *
|
---|
| 11 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 12 | * found in the LICENSE file at https://angular.io/license
|
---|
| 13 | */
|
---|
| 14 | /**
|
---|
| 15 | * @fileoverview
|
---|
| 16 | * @suppress {globalThis,undefinedVars}
|
---|
| 17 | */
|
---|
| 18 | Zone.__load_patch('Error', (global, Zone, api) => {
|
---|
| 19 | /*
|
---|
| 20 | * This code patches Error so that:
|
---|
| 21 | * - It ignores un-needed stack frames.
|
---|
| 22 | * - It Shows the associated Zone for reach frame.
|
---|
| 23 | */
|
---|
| 24 | const zoneJsInternalStackFramesSymbol = api.symbol('zoneJsInternalStackFrames');
|
---|
| 25 | const NativeError = global[api.symbol('Error')] = global['Error'];
|
---|
| 26 | // Store the frames which should be removed from the stack frames
|
---|
| 27 | const zoneJsInternalStackFrames = {};
|
---|
| 28 | // We must find the frame where Error was created, otherwise we assume we don't understand stack
|
---|
| 29 | let zoneAwareFrame1;
|
---|
| 30 | let zoneAwareFrame2;
|
---|
| 31 | let zoneAwareFrame1WithoutNew;
|
---|
| 32 | let zoneAwareFrame2WithoutNew;
|
---|
| 33 | let zoneAwareFrame3WithoutNew;
|
---|
| 34 | global['Error'] = ZoneAwareError;
|
---|
| 35 | const stackRewrite = 'stackRewrite';
|
---|
| 36 | const zoneJsInternalStackFramesPolicy = global['__Zone_Error_BlacklistedStackFrames_policy'] ||
|
---|
| 37 | global['__Zone_Error_ZoneJsInternalStackFrames_policy'] || 'default';
|
---|
| 38 | function buildZoneFrameNames(zoneFrame) {
|
---|
| 39 | let zoneFrameName = { zoneName: zoneFrame.zone.name };
|
---|
| 40 | let result = zoneFrameName;
|
---|
| 41 | while (zoneFrame.parent) {
|
---|
| 42 | zoneFrame = zoneFrame.parent;
|
---|
| 43 | const parentZoneFrameName = { zoneName: zoneFrame.zone.name };
|
---|
| 44 | zoneFrameName.parent = parentZoneFrameName;
|
---|
| 45 | zoneFrameName = parentZoneFrameName;
|
---|
| 46 | }
|
---|
| 47 | return result;
|
---|
| 48 | }
|
---|
| 49 | function buildZoneAwareStackFrames(originalStack, zoneFrame, isZoneFrame = true) {
|
---|
| 50 | let frames = originalStack.split('\n');
|
---|
| 51 | let i = 0;
|
---|
| 52 | // Find the first frame
|
---|
| 53 | while (!(frames[i] === zoneAwareFrame1 || frames[i] === zoneAwareFrame2 ||
|
---|
| 54 | frames[i] === zoneAwareFrame1WithoutNew || frames[i] === zoneAwareFrame2WithoutNew ||
|
---|
| 55 | frames[i] === zoneAwareFrame3WithoutNew) &&
|
---|
| 56 | i < frames.length) {
|
---|
| 57 | i++;
|
---|
| 58 | }
|
---|
| 59 | for (; i < frames.length && zoneFrame; i++) {
|
---|
| 60 | let frame = frames[i];
|
---|
| 61 | if (frame.trim()) {
|
---|
| 62 | switch (zoneJsInternalStackFrames[frame]) {
|
---|
| 63 | case 0 /* zoneJsInternal */:
|
---|
| 64 | frames.splice(i, 1);
|
---|
| 65 | i--;
|
---|
| 66 | break;
|
---|
| 67 | case 1 /* transition */:
|
---|
| 68 | if (zoneFrame.parent) {
|
---|
| 69 | // This is the special frame where zone changed. Print and process it accordingly
|
---|
| 70 | zoneFrame = zoneFrame.parent;
|
---|
| 71 | }
|
---|
| 72 | else {
|
---|
| 73 | zoneFrame = null;
|
---|
| 74 | }
|
---|
| 75 | frames.splice(i, 1);
|
---|
| 76 | i--;
|
---|
| 77 | break;
|
---|
| 78 | default:
|
---|
| 79 | frames[i] += isZoneFrame ? ` [${zoneFrame.zone.name}]` :
|
---|
| 80 | ` [${zoneFrame.zoneName}]`;
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 | return frames.join('\n');
|
---|
| 85 | }
|
---|
| 86 | /**
|
---|
| 87 | * This is ZoneAwareError which processes the stack frame and cleans up extra frames as well as
|
---|
| 88 | * adds zone information to it.
|
---|
| 89 | */
|
---|
| 90 | function ZoneAwareError() {
|
---|
| 91 | // We always have to return native error otherwise the browser console will not work.
|
---|
| 92 | let error = NativeError.apply(this, arguments);
|
---|
| 93 | // Save original stack trace
|
---|
| 94 | const originalStack = error['originalStack'] = error.stack;
|
---|
| 95 | // Process the stack trace and rewrite the frames.
|
---|
| 96 | if (ZoneAwareError[stackRewrite] && originalStack) {
|
---|
| 97 | let zoneFrame = api.currentZoneFrame();
|
---|
| 98 | if (zoneJsInternalStackFramesPolicy === 'lazy') {
|
---|
| 99 | // don't handle stack trace now
|
---|
| 100 | error[api.symbol('zoneFrameNames')] = buildZoneFrameNames(zoneFrame);
|
---|
| 101 | }
|
---|
| 102 | else if (zoneJsInternalStackFramesPolicy === 'default') {
|
---|
| 103 | try {
|
---|
| 104 | error.stack = error.zoneAwareStack = buildZoneAwareStackFrames(originalStack, zoneFrame);
|
---|
| 105 | }
|
---|
| 106 | catch (e) {
|
---|
| 107 | // ignore as some browsers don't allow overriding of stack
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | if (this instanceof NativeError && this.constructor != NativeError) {
|
---|
| 112 | // We got called with a `new` operator AND we are subclass of ZoneAwareError
|
---|
| 113 | // in that case we have to copy all of our properties to `this`.
|
---|
| 114 | Object.keys(error).concat('stack', 'message').forEach((key) => {
|
---|
| 115 | const value = error[key];
|
---|
| 116 | if (value !== undefined) {
|
---|
| 117 | try {
|
---|
| 118 | this[key] = value;
|
---|
| 119 | }
|
---|
| 120 | catch (e) {
|
---|
| 121 | // ignore the assignment in case it is a setter and it throws.
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | });
|
---|
| 125 | return this;
|
---|
| 126 | }
|
---|
| 127 | return error;
|
---|
| 128 | }
|
---|
| 129 | // Copy the prototype so that instanceof operator works as expected
|
---|
| 130 | ZoneAwareError.prototype = NativeError.prototype;
|
---|
| 131 | ZoneAwareError[zoneJsInternalStackFramesSymbol] = zoneJsInternalStackFrames;
|
---|
| 132 | ZoneAwareError[stackRewrite] = false;
|
---|
| 133 | const zoneAwareStackSymbol = api.symbol('zoneAwareStack');
|
---|
| 134 | // try to define zoneAwareStack property when zoneJsInternal frames policy is delay
|
---|
| 135 | if (zoneJsInternalStackFramesPolicy === 'lazy') {
|
---|
| 136 | Object.defineProperty(ZoneAwareError.prototype, 'zoneAwareStack', {
|
---|
| 137 | configurable: true,
|
---|
| 138 | enumerable: true,
|
---|
| 139 | get: function () {
|
---|
| 140 | if (!this[zoneAwareStackSymbol]) {
|
---|
| 141 | this[zoneAwareStackSymbol] = buildZoneAwareStackFrames(this.originalStack, this[api.symbol('zoneFrameNames')], false);
|
---|
| 142 | }
|
---|
| 143 | return this[zoneAwareStackSymbol];
|
---|
| 144 | },
|
---|
| 145 | set: function (newStack) {
|
---|
| 146 | this.originalStack = newStack;
|
---|
| 147 | this[zoneAwareStackSymbol] = buildZoneAwareStackFrames(this.originalStack, this[api.symbol('zoneFrameNames')], false);
|
---|
| 148 | }
|
---|
| 149 | });
|
---|
| 150 | }
|
---|
| 151 | // those properties need special handling
|
---|
| 152 | const specialPropertyNames = ['stackTraceLimit', 'captureStackTrace', 'prepareStackTrace'];
|
---|
| 153 | // those properties of NativeError should be set to ZoneAwareError
|
---|
| 154 | const nativeErrorProperties = Object.keys(NativeError);
|
---|
| 155 | if (nativeErrorProperties) {
|
---|
| 156 | nativeErrorProperties.forEach(prop => {
|
---|
| 157 | if (specialPropertyNames.filter(sp => sp === prop).length === 0) {
|
---|
| 158 | Object.defineProperty(ZoneAwareError, prop, {
|
---|
| 159 | get: function () {
|
---|
| 160 | return NativeError[prop];
|
---|
| 161 | },
|
---|
| 162 | set: function (value) {
|
---|
| 163 | NativeError[prop] = value;
|
---|
| 164 | }
|
---|
| 165 | });
|
---|
| 166 | }
|
---|
| 167 | });
|
---|
| 168 | }
|
---|
| 169 | if (NativeError.hasOwnProperty('stackTraceLimit')) {
|
---|
| 170 | // Extend default stack limit as we will be removing few frames.
|
---|
| 171 | NativeError.stackTraceLimit = Math.max(NativeError.stackTraceLimit, 15);
|
---|
| 172 | // make sure that ZoneAwareError has the same property which forwards to NativeError.
|
---|
| 173 | Object.defineProperty(ZoneAwareError, 'stackTraceLimit', {
|
---|
| 174 | get: function () {
|
---|
| 175 | return NativeError.stackTraceLimit;
|
---|
| 176 | },
|
---|
| 177 | set: function (value) {
|
---|
| 178 | return NativeError.stackTraceLimit = value;
|
---|
| 179 | }
|
---|
| 180 | });
|
---|
| 181 | }
|
---|
| 182 | if (NativeError.hasOwnProperty('captureStackTrace')) {
|
---|
| 183 | Object.defineProperty(ZoneAwareError, 'captureStackTrace', {
|
---|
| 184 | // add named function here because we need to remove this
|
---|
| 185 | // stack frame when prepareStackTrace below
|
---|
| 186 | value: function zoneCaptureStackTrace(targetObject, constructorOpt) {
|
---|
| 187 | NativeError.captureStackTrace(targetObject, constructorOpt);
|
---|
| 188 | }
|
---|
| 189 | });
|
---|
| 190 | }
|
---|
| 191 | const ZONE_CAPTURESTACKTRACE = 'zoneCaptureStackTrace';
|
---|
| 192 | Object.defineProperty(ZoneAwareError, 'prepareStackTrace', {
|
---|
| 193 | get: function () {
|
---|
| 194 | return NativeError.prepareStackTrace;
|
---|
| 195 | },
|
---|
| 196 | set: function (value) {
|
---|
| 197 | if (!value || typeof value !== 'function') {
|
---|
| 198 | return NativeError.prepareStackTrace = value;
|
---|
| 199 | }
|
---|
| 200 | return NativeError.prepareStackTrace = function (error, structuredStackTrace) {
|
---|
| 201 | // remove additional stack information from ZoneAwareError.captureStackTrace
|
---|
| 202 | if (structuredStackTrace) {
|
---|
| 203 | for (let i = 0; i < structuredStackTrace.length; i++) {
|
---|
| 204 | const st = structuredStackTrace[i];
|
---|
| 205 | // remove the first function which name is zoneCaptureStackTrace
|
---|
| 206 | if (st.getFunctionName() === ZONE_CAPTURESTACKTRACE) {
|
---|
| 207 | structuredStackTrace.splice(i, 1);
|
---|
| 208 | break;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | return value.call(this, error, structuredStackTrace);
|
---|
| 213 | };
|
---|
| 214 | }
|
---|
| 215 | });
|
---|
| 216 | if (zoneJsInternalStackFramesPolicy === 'disable') {
|
---|
| 217 | // don't need to run detectZone to populate zoneJs internal stack frames
|
---|
| 218 | return;
|
---|
| 219 | }
|
---|
| 220 | // Now we need to populate the `zoneJsInternalStackFrames` as well as find the
|
---|
| 221 | // run/runGuarded/runTask frames. This is done by creating a detect zone and then threading
|
---|
| 222 | // the execution through all of the above methods so that we can look at the stack trace and
|
---|
| 223 | // find the frames of interest.
|
---|
| 224 | let detectZone = Zone.current.fork({
|
---|
| 225 | name: 'detect',
|
---|
| 226 | onHandleError: function (parentZD, current, target, error) {
|
---|
| 227 | if (error.originalStack && Error === ZoneAwareError) {
|
---|
| 228 | let frames = error.originalStack.split(/\n/);
|
---|
| 229 | let runFrame = false, runGuardedFrame = false, runTaskFrame = false;
|
---|
| 230 | while (frames.length) {
|
---|
| 231 | let frame = frames.shift();
|
---|
| 232 | // On safari it is possible to have stack frame with no line number.
|
---|
| 233 | // This check makes sure that we don't filter frames on name only (must have
|
---|
| 234 | // line number or exact equals to `ZoneAwareError`)
|
---|
| 235 | if (/:\d+:\d+/.test(frame) || frame === 'ZoneAwareError') {
|
---|
| 236 | // Get rid of the path so that we don't accidentally find function name in path.
|
---|
| 237 | // In chrome the separator is `(` and `@` in FF and safari
|
---|
| 238 | // Chrome: at Zone.run (zone.js:100)
|
---|
| 239 | // Chrome: at Zone.run (http://localhost:9876/base/build/lib/zone.js:100:24)
|
---|
| 240 | // FireFox: Zone.prototype.run@http://localhost:9876/base/build/lib/zone.js:101:24
|
---|
| 241 | // Safari: run@http://localhost:9876/base/build/lib/zone.js:101:24
|
---|
| 242 | let fnName = frame.split('(')[0].split('@')[0];
|
---|
| 243 | let frameType = 1 /* transition */;
|
---|
| 244 | if (fnName.indexOf('ZoneAwareError') !== -1) {
|
---|
| 245 | if (fnName.indexOf('new ZoneAwareError') !== -1) {
|
---|
| 246 | zoneAwareFrame1 = frame;
|
---|
| 247 | zoneAwareFrame2 = frame.replace('new ZoneAwareError', 'new Error.ZoneAwareError');
|
---|
| 248 | }
|
---|
| 249 | else {
|
---|
| 250 | zoneAwareFrame1WithoutNew = frame;
|
---|
| 251 | zoneAwareFrame2WithoutNew = frame.replace('Error.', '');
|
---|
| 252 | if (frame.indexOf('Error.ZoneAwareError') === -1) {
|
---|
| 253 | zoneAwareFrame3WithoutNew =
|
---|
| 254 | frame.replace('ZoneAwareError', 'Error.ZoneAwareError');
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | zoneJsInternalStackFrames[zoneAwareFrame2] = 0 /* zoneJsInternal */;
|
---|
| 258 | }
|
---|
| 259 | if (fnName.indexOf('runGuarded') !== -1) {
|
---|
| 260 | runGuardedFrame = true;
|
---|
| 261 | }
|
---|
| 262 | else if (fnName.indexOf('runTask') !== -1) {
|
---|
| 263 | runTaskFrame = true;
|
---|
| 264 | }
|
---|
| 265 | else if (fnName.indexOf('run') !== -1) {
|
---|
| 266 | runFrame = true;
|
---|
| 267 | }
|
---|
| 268 | else {
|
---|
| 269 | frameType = 0 /* zoneJsInternal */;
|
---|
| 270 | }
|
---|
| 271 | zoneJsInternalStackFrames[frame] = frameType;
|
---|
| 272 | // Once we find all of the frames we can stop looking.
|
---|
| 273 | if (runFrame && runGuardedFrame && runTaskFrame) {
|
---|
| 274 | ZoneAwareError[stackRewrite] = true;
|
---|
| 275 | break;
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | return false;
|
---|
| 281 | }
|
---|
| 282 | });
|
---|
| 283 | // carefully constructor a stack frame which contains all of the frames of interest which
|
---|
| 284 | // need to be detected and marked as an internal zoneJs frame.
|
---|
| 285 | const childDetectZone = detectZone.fork({
|
---|
| 286 | name: 'child',
|
---|
| 287 | onScheduleTask: function (delegate, curr, target, task) {
|
---|
| 288 | return delegate.scheduleTask(target, task);
|
---|
| 289 | },
|
---|
| 290 | onInvokeTask: function (delegate, curr, target, task, applyThis, applyArgs) {
|
---|
| 291 | return delegate.invokeTask(target, task, applyThis, applyArgs);
|
---|
| 292 | },
|
---|
| 293 | onCancelTask: function (delegate, curr, target, task) {
|
---|
| 294 | return delegate.cancelTask(target, task);
|
---|
| 295 | },
|
---|
| 296 | onInvoke: function (delegate, curr, target, callback, applyThis, applyArgs, source) {
|
---|
| 297 | return delegate.invoke(target, callback, applyThis, applyArgs, source);
|
---|
| 298 | }
|
---|
| 299 | });
|
---|
| 300 | // we need to detect all zone related frames, it will
|
---|
| 301 | // exceed default stackTraceLimit, so we set it to
|
---|
| 302 | // larger number here, and restore it after detect finish.
|
---|
| 303 | // We cast through any so we don't need to depend on nodejs typings.
|
---|
| 304 | const originalStackTraceLimit = Error.stackTraceLimit;
|
---|
| 305 | Error.stackTraceLimit = 100;
|
---|
| 306 | // we schedule event/micro/macro task, and invoke them
|
---|
| 307 | // when onSchedule, so we can get all stack traces for
|
---|
| 308 | // all kinds of tasks with one error thrown.
|
---|
| 309 | childDetectZone.run(() => {
|
---|
| 310 | childDetectZone.runGuarded(() => {
|
---|
| 311 | const fakeTransitionTo = () => { };
|
---|
| 312 | childDetectZone.scheduleEventTask(zoneJsInternalStackFramesSymbol, () => {
|
---|
| 313 | childDetectZone.scheduleMacroTask(zoneJsInternalStackFramesSymbol, () => {
|
---|
| 314 | childDetectZone.scheduleMicroTask(zoneJsInternalStackFramesSymbol, () => {
|
---|
| 315 | throw new Error();
|
---|
| 316 | }, undefined, (t) => {
|
---|
| 317 | t._transitionTo = fakeTransitionTo;
|
---|
| 318 | t.invoke();
|
---|
| 319 | });
|
---|
| 320 | childDetectZone.scheduleMicroTask(zoneJsInternalStackFramesSymbol, () => {
|
---|
| 321 | throw Error();
|
---|
| 322 | }, undefined, (t) => {
|
---|
| 323 | t._transitionTo = fakeTransitionTo;
|
---|
| 324 | t.invoke();
|
---|
| 325 | });
|
---|
| 326 | }, undefined, (t) => {
|
---|
| 327 | t._transitionTo = fakeTransitionTo;
|
---|
| 328 | t.invoke();
|
---|
| 329 | }, () => { });
|
---|
| 330 | }, undefined, (t) => {
|
---|
| 331 | t._transitionTo = fakeTransitionTo;
|
---|
| 332 | t.invoke();
|
---|
| 333 | }, () => { });
|
---|
| 334 | });
|
---|
| 335 | });
|
---|
| 336 | Error.stackTraceLimit = originalStackTraceLimit;
|
---|
| 337 | });
|
---|