zone-error.umd.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. 'use strict';
  2. /**
  3. * @license Angular v<unknown>
  4. * (c) 2010-2022 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. (function (factory) {
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. factory();
  10. })((function () {
  11. 'use strict';
  12. /**
  13. * @fileoverview
  14. * @suppress {globalThis,undefinedVars}
  15. */
  16. Zone.__load_patch('Error', function (global, Zone, api) {
  17. /*
  18. * This code patches Error so that:
  19. * - It ignores un-needed stack frames.
  20. * - It Shows the associated Zone for reach frame.
  21. */
  22. var zoneJsInternalStackFramesSymbol = api.symbol('zoneJsInternalStackFrames');
  23. var NativeError = global[api.symbol('Error')] = global['Error'];
  24. // Store the frames which should be removed from the stack frames
  25. var zoneJsInternalStackFrames = {};
  26. // We must find the frame where Error was created, otherwise we assume we don't understand stack
  27. var zoneAwareFrame1;
  28. var zoneAwareFrame2;
  29. var zoneAwareFrame1WithoutNew;
  30. var zoneAwareFrame2WithoutNew;
  31. var zoneAwareFrame3WithoutNew;
  32. global['Error'] = ZoneAwareError;
  33. var stackRewrite = 'stackRewrite';
  34. var zoneJsInternalStackFramesPolicy = global['__Zone_Error_BlacklistedStackFrames_policy'] ||
  35. global['__Zone_Error_ZoneJsInternalStackFrames_policy'] || 'default';
  36. function buildZoneFrameNames(zoneFrame) {
  37. var zoneFrameName = { zoneName: zoneFrame.zone.name };
  38. var result = zoneFrameName;
  39. while (zoneFrame.parent) {
  40. zoneFrame = zoneFrame.parent;
  41. var parentZoneFrameName = { zoneName: zoneFrame.zone.name };
  42. zoneFrameName.parent = parentZoneFrameName;
  43. zoneFrameName = parentZoneFrameName;
  44. }
  45. return result;
  46. }
  47. function buildZoneAwareStackFrames(originalStack, zoneFrame, isZoneFrame) {
  48. if (isZoneFrame === void 0) { isZoneFrame = true; }
  49. var frames = originalStack.split('\n');
  50. var i = 0;
  51. // Find the first frame
  52. while (!(frames[i] === zoneAwareFrame1 || frames[i] === zoneAwareFrame2 ||
  53. frames[i] === zoneAwareFrame1WithoutNew || frames[i] === zoneAwareFrame2WithoutNew ||
  54. frames[i] === zoneAwareFrame3WithoutNew) &&
  55. i < frames.length) {
  56. i++;
  57. }
  58. for (; i < frames.length && zoneFrame; i++) {
  59. var frame = frames[i];
  60. if (frame.trim()) {
  61. switch (zoneJsInternalStackFrames[frame]) {
  62. case 0 /* FrameType.zoneJsInternal */:
  63. frames.splice(i, 1);
  64. i--;
  65. break;
  66. case 1 /* FrameType.transition */:
  67. if (zoneFrame.parent) {
  68. // This is the special frame where zone changed. Print and process it accordingly
  69. zoneFrame = zoneFrame.parent;
  70. }
  71. else {
  72. zoneFrame = null;
  73. }
  74. frames.splice(i, 1);
  75. i--;
  76. break;
  77. default:
  78. frames[i] += isZoneFrame ? " [".concat(zoneFrame.zone.name, "]") :
  79. " [".concat(zoneFrame.zoneName, "]");
  80. }
  81. }
  82. }
  83. return frames.join('\n');
  84. }
  85. /**
  86. * This is ZoneAwareError which processes the stack frame and cleans up extra frames as well as
  87. * adds zone information to it.
  88. */
  89. function ZoneAwareError() {
  90. var _this = this;
  91. // We always have to return native error otherwise the browser console will not work.
  92. var error = NativeError.apply(this, arguments);
  93. // Save original stack trace
  94. var originalStack = error['originalStack'] = error.stack;
  95. // Process the stack trace and rewrite the frames.
  96. if (ZoneAwareError[stackRewrite] && originalStack) {
  97. var 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(function (key) {
  115. var 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. var 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. var specialPropertyNames = ['stackTraceLimit', 'captureStackTrace', 'prepareStackTrace'];
  153. // those properties of NativeError should be set to ZoneAwareError
  154. var nativeErrorProperties = Object.keys(NativeError);
  155. if (nativeErrorProperties) {
  156. nativeErrorProperties.forEach(function (prop) {
  157. if (specialPropertyNames.filter(function (sp) { return 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. var 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 (var i = 0; i < structuredStackTrace.length; i++) {
  204. var 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. var detectZone = Zone.current.fork({
  225. name: 'detect',
  226. onHandleError: function (parentZD, current, target, error) {
  227. if (error.originalStack && Error === ZoneAwareError) {
  228. var frames_1 = error.originalStack.split(/\n/);
  229. var runFrame = false, runGuardedFrame = false, runTaskFrame = false;
  230. while (frames_1.length) {
  231. var frame = frames_1.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. var fnName = frame.split('(')[0].split('@')[0];
  243. var frameType = 1 /* FrameType.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 /* FrameType.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 /* FrameType.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. var 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. var 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(function () {
  310. childDetectZone.runGuarded(function () {
  311. var fakeTransitionTo = function () { };
  312. childDetectZone.scheduleEventTask(zoneJsInternalStackFramesSymbol, function () {
  313. childDetectZone.scheduleMacroTask(zoneJsInternalStackFramesSymbol, function () {
  314. childDetectZone.scheduleMicroTask(zoneJsInternalStackFramesSymbol, function () {
  315. throw new Error();
  316. }, undefined, function (t) {
  317. t._transitionTo = fakeTransitionTo;
  318. t.invoke();
  319. });
  320. childDetectZone.scheduleMicroTask(zoneJsInternalStackFramesSymbol, function () {
  321. throw Error();
  322. }, undefined, function (t) {
  323. t._transitionTo = fakeTransitionTo;
  324. t.invoke();
  325. });
  326. }, undefined, function (t) {
  327. t._transitionTo = fakeTransitionTo;
  328. t.invoke();
  329. }, function () { });
  330. }, undefined, function (t) {
  331. t._transitionTo = fakeTransitionTo;
  332. t.invoke();
  333. }, function () { });
  334. });
  335. });
  336. Error.stackTraceLimit = originalStackTraceLimit;
  337. });
  338. }));