Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TraceKit can now be used on frames #68

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 43 additions & 29 deletions tracekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,38 @@ TraceKit.report = (function reportModuleWrapper() {
lastException = null,
lastExceptionStack = null;

function isWindowAccessible(win) {
try {
return (win.location.host);
} catch (e) {}
}
/**
* Add a crash handler.
* @param {Function} handler
* @memberof TraceKit.report
*/
function subscribe(handler) {
installGlobalHandler();
handlers.push(handler);
function subscribe(handler, aWindow) {
aWindow = (aWindow || window);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename aWindow to win it's shorter and you know what it does, it's also consistent with your function above.

if (isWindowAccessible(aWindow)) {
TraceKit.windowPointer = aWindow;
installGlobalHandler(handler, aWindow);
}
}

/**
* Remove a crash handler.
* @param {Function} handler
* @memberof TraceKit.report
*/
function unsubscribe(handler) {
function unsubscribe(handler, aWindow) {
aWindow = (aWindow || window);
if (isWindowAccessible(aWindow)) {
for (var i = handlers.length - 1; i >= 0; --i) {
if (handlers[i] === handler) {
if (handlers[i][0] === handler && aWindow === handlers[i][1]) {
handlers.splice(i, 1);
}
}
}
}

/**
Expand All @@ -149,27 +160,29 @@ TraceKit.report = (function reportModuleWrapper() {
* @memberof TraceKit.report
* @throws An exception if an error occurs while calling an handler.
*/
function notifyHandlers(stack, isWindowError, error) {
function notifyHandlers(stack, isWindowError, aArguments) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does arguments contain? Can we call this args? and maybe a comment to break down what the possible structure is.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, arguments contains all the arguments from onerror event

var exception = null;
if (isWindowError && !TraceKit.collectWindowErrors) {
return;
}
for (var i in handlers) {
if (_has(handlers, i)) {
if (_has(handlers, i) && handlers[i][1] === TraceKit.windowPointer) {
try {
handlers[i](stack, isWindowError, error);
handlers[i][0](stack, isWindowError, (aArguments.length > 4) ? aArguments[4] : null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the 3rd and 4th parameter here? This became much less readable.

} catch (inner) {
exception = inner;
}
if (handlers[i][1]._oldOnerrorHandler) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was a local variable.

return handlers[i][1]._oldOnerrorHandler.apply(handlers[i][1], aArguments);
}
}
}

if (exception) {
throw exception;
}
}

var _oldOnerrorHandler, _onErrorHandlerInstalled;

/**
* Ensures all global unhandled exceptions are recorded.
Expand All @@ -183,13 +196,13 @@ TraceKit.report = (function reportModuleWrapper() {
*/
function traceKitWindowOnError(message, url, lineNo, columnNo, errorObj) {
var stack = null;

TraceKit.windowPointer = this;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this modifies the pointer which might of been set earlier when you subscribed. I'm not sure this should be updated as it would change the subscription handler? Thoughts?

if (lastExceptionStack) {
TraceKit.computeStackTrace.augmentStackTraceWithInitialElement(lastExceptionStack, url, lineNo, message);
processLastException();
processLastException(arguments);
} else if (errorObj) {
stack = TraceKit.computeStackTrace(errorObj);
notifyHandlers(stack, true, errorObj);
notifyHandlers(stack, true, arguments);
} else {
var location = {
'url': url,
Expand All @@ -204,12 +217,10 @@ TraceKit.report = (function reportModuleWrapper() {
'stack': [location]
};

notifyHandlers(stack, true, null);
notifyHandlers(stack, true, arguments);
}

if (_oldOnerrorHandler) {
return _oldOnerrorHandler.apply(this, arguments);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we were calling old handlers to preserve existing work flows.

}


return false;
}
Expand All @@ -218,13 +229,14 @@ TraceKit.report = (function reportModuleWrapper() {
* Install a global onerror handler
* @memberof TraceKit.report
*/
function installGlobalHandler () {
if (_onErrorHandlerInstalled === true) {
function installGlobalHandler(aHandlers, aWindow) {
if (aWindow._onErrorHandlerInstalled === true) {
return;
}
_oldOnerrorHandler = window.onerror;
window.onerror = traceKitWindowOnError;
_onErrorHandlerInstalled = true;
var _oldOnerrorHandler = aWindow.onerror;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is no longer a private field we need to change the variable name

aWindow.onerror = traceKitWindowOnError;
aWindow._onErrorHandlerInstalled = true;
handlers.push([aHandlers, aWindow, _oldOnerrorHandler]);
}

/**
Expand Down Expand Up @@ -419,7 +431,9 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
*/
var source = '';
var domain = '';
try { domain = window.document.domain; } catch (e) { }
try {
domain = window.document.domain;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the pointer here>

} catch (e) {}
var match = /(.*)\:\/\/([^:\/]+)([:\d]*)\/{0,1}([\s\S]*)/.exec(url);
if (match && match[2] === domain) {
source = loadSource(url);
Expand Down Expand Up @@ -590,12 +604,12 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
* @memberof TraceKit.computeStackTrace
*/
function findSourceByFunctionBody(func) {
if (_isUndefined(window && window.document)) {
if (_isUndefined(TraceKit.windowPointer && window.document)) {
return;
}

var urls = [window.location.href],
scripts = window.document.getElementsByTagName('script'),
var urls = [TraceKit.windowPointer.location.href],
scripts = TraceKit.windowPointer.document.getElementsByTagName('script'),
body,
code = '' + func,
codeRE = /^function(?:\s+([\w$]+))?\s*\(([\w\s,]*)\)\s*\{\s*(\S[\s\S]*\S)\s*\}\s*$/,
Expand Down Expand Up @@ -880,7 +894,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
lineRE2 = /^\s*Line (\d+) of inline#(\d+) script in ((?:file|https?|blob)\S+)(?:: in function (\S+))?\s*$/i,
lineRE3 = /^\s*Line (\d+) of function script\s*$/i,
stack = [],
scripts = (window && window.document && window.document.getElementsByTagName('script')),
scripts = (TraceKit.windowPointer && TraceKit.windowPointer.document && TraceKit.windowPointer.document.getElementsByTagName('script')),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this window pointer to a local variable, this will not minify very well.

inlineScriptBlocks = [],
parts;

Expand Down Expand Up @@ -921,7 +935,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
}
}
} else if ((parts = lineRE3.exec(lines[line]))) {
var url = window.location.href.replace(/#.*$/, '');
var url = TraceKit.windowPointer.location.href.replace(/#.*$/, '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about parsing source in the inner frame?

var re = new RegExp(escapeCodeAsRegExpForMatchingInsideHTML(lines[line + 1]));
var src = findSourceInUrls(re, [url]);
item = {
Expand Down Expand Up @@ -1195,8 +1209,8 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
*/
TraceKit.extendToAsynchronousCallbacks = function () {
var _helper = function _helper(fnName) {
var originalFn = window[fnName];
window[fnName] = function traceKitAsyncExtension() {
var originalFn = TraceKit.windowPointer[fnName];
TraceKit.windowPointer[fnName] = function traceKitAsyncExtension() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to store this in a variable

// Make a copy of the arguments
var args = _slice.call(arguments);
var originalCallback = args[0];
Expand Down