zone-error.umd.js 19 KB

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