From a01c2896eb3d82dc4704b4c98f91822af6edd1f2 Mon Sep 17 00:00:00 2001 From: Tim Lancina Date: Thu, 11 Jun 2015 12:33:40 -0500 Subject: [PATCH] get in the zone --- scripts/e2e/ionic.template.html | 9 +-- scripts/resources/long-stack-trace-zone.js | 88 ++++++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 scripts/resources/long-stack-trace-zone.js diff --git a/scripts/e2e/ionic.template.html b/scripts/e2e/ionic.template.html index acabaf6b2c..3b51532362 100644 --- a/scripts/e2e/ionic.template.html +++ b/scripts/e2e/ionic.template.html @@ -22,12 +22,7 @@ - - - - @@ -36,6 +31,8 @@ + + diff --git a/scripts/resources/long-stack-trace-zone.js b/scripts/resources/long-stack-trace-zone.js new file mode 100644 index 0000000000..47d1c127de --- /dev/null +++ b/scripts/resources/long-stack-trace-zone.js @@ -0,0 +1,88 @@ +/* + * Wrapped stacktrace + * + * We need this because in some implementations, constructing a trace is slow + * and so we want to defer accessing the trace for as long as possible + */ +Zone.Stacktrace = function (e) { + this._e = e; +}; +Zone.Stacktrace.prototype.get = function () { + if (zone.stackFramesFilter) { + return this._e.stack. + split('\n'). + filter(zone.stackFramesFilter). + join('\n'); + } + return this._e.stack; +} + +Zone.getStacktrace = function () { + function getStacktraceWithUncaughtError () { + return new Zone.Stacktrace(new Error()); + } + + function getStacktraceWithCaughtError () { + try { + throw new Error(); + } catch (e) { + return new Zone.Stacktrace(e); + } + } + + // Some implementations of exception handling don't create a stack trace if the exception + // isn't thrown, however it's faster not to actually throw the exception. + var stack = getStacktraceWithUncaughtError(); + if (stack && stack._e.stack) { + Zone.getStacktrace = getStacktraceWithUncaughtError; + return stack; + } else { + Zone.getStacktrace = getStacktraceWithCaughtError; + return Zone.getStacktrace(); + } +}; + +Zone.longStackTraceZone = { + getLongStacktrace: function (exception) { + var trace = []; + var zone = this; + if (exception) { + if (zone.stackFramesFilter) { + trace.push(exception.stack.split('\n'). + filter(zone.stackFramesFilter). + join('\n')); + } else { + trace.push(exception.stack); + } + } + var now = Date.now(); + while (zone && zone.constructedAtException) { + trace.push( + '--- ' + (Date(zone.constructedAtTime)).toString() + + ' - ' + (now - zone.constructedAtTime) + 'ms ago', + zone.constructedAtException.get()); + zone = zone.parent; + } + return trace.join('\n'); + }, + + stackFramesFilter: function (line) { + return line.indexOf('zone.js') === -1; + }, + + onError: function (exception) { + var reporter = this.reporter || console.log.bind(console); + reporter(exception.toString()); + reporter(this.getLongStacktrace(exception)); + }, + + '$fork': function (parentFork) { + return function() { + var newZone = parentFork.apply(this, arguments); + newZone.constructedAtException = Zone.getStacktrace(); + newZone.constructedAtTime = Date.now(); + return newZone; + } + } +}; +