Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
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
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-tactile
80 changes: 49 additions & 31 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, win) {
win = (win || window);
if (isWindowAccessible(win)) {
TraceKit.windowPointer = win;
installGlobalHandler(handler, win);
}
}

/**
* Remove a crash handler.
* @param {Function} handler
* @memberof TraceKit.report
*/
function unsubscribe(handler) {
function unsubscribe(handler, win) {
win = (win || window);
if (isWindowAccessible(win)) {
for (var i = handlers.length - 1; i >= 0; --i) {
if (handlers[i] === handler) {
if (handlers[i][0] === handler && win === handlers[i][1]) {
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 all stored on handler now in this array?

Copy link
Collaborator

Choose a reason for hiding this comment

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

We weren't doing this before, but since you are calling unsubscribe and you have an array with the previous error handler. Do you think we should be setting it back?

Copy link
Author

Choose a reason for hiding this comment

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

Good point, i will insert the code

handlers.splice(i, 1);
}
}
}
}

/**
Expand All @@ -149,27 +160,31 @@ 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);
var errorObj=(aArguments.length > 4) ? aArguments[4] : null;
handlers[i][0](stack, isWindowError, errorObj);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is kind of confusing, so we call a method with stack, isWindowErorr and an array with 4 items? We seem to only be using the error object, so it might be better to revert this signature to just be error object. Previous thought: Maybe we need to add a comment stating that these arguments are actually: message, url, lineNo, columnNo, errorObj (if we were on es6, restructuring would make this so much more readable but we can't use that :(`,

} catch (inner) {
exception = inner;
}
// Call old onerror events
if (handlers[i][2]) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We weren't doing this before were we? It's just really hard to follow this and make sense of what's going on.. We call all the other handlers in a try catch.

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

if (exception) {
throw exception;
}
}

var _oldOnerrorHandler, _onErrorHandlerInstalled;

/**
* Ensures all global unhandled exceptions are recorded.
Expand All @@ -183,13 +198,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,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason for removing this?

Copy link
Author

Choose a reason for hiding this comment

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

is moved to notifyHandlers

Expand All @@ -204,12 +219,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 +231,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;
aWindow.onerror = traceKitWindowOnError;
aWindow._onErrorHandlerInstalled = true;
handlers.push([aHandlers, aWindow, oldOnerrorHandler]);
}

/**
Expand Down Expand Up @@ -361,9 +375,10 @@ TraceKit.report = (function reportModuleWrapper() {
* @memberof TraceKit
* @namespace
*/
TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
TraceKit.computeStackTrace = (function() {
var debug = false,
sourceCache = {};
sourceCache = {},
curWin = TraceKit.windowPointer;
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 this to win


/**
* Attempts to retrieve source code via XMLHttpRequest, which is used
Expand Down Expand Up @@ -419,7 +434,9 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
*/
var source = '';
var domain = '';
try { domain = window.document.domain; } catch (e) { }
try {
domain = curWin.document.domain;
} 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 +607,12 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
* @memberof TraceKit.computeStackTrace
*/
function findSourceByFunctionBody(func) {
if (_isUndefined(window && window.document)) {
if (_isUndefined(curWin && curWin.document)) {
return;
}

var urls = [window.location.href],
scripts = window.document.getElementsByTagName('script'),
var urls = [curWin.location.href],
scripts = curWin.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 +897,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 = (curWin && curWin.document && curWin.document.getElementsByTagName('script')),
inlineScriptBlocks = [],
parts;

Expand Down Expand Up @@ -921,7 +938,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
}
}
} else if ((parts = lineRE3.exec(lines[line]))) {
var url = window.location.href.replace(/#.*$/, '');
var url = curWin.location.href.replace(/#.*$/, '');
var re = new RegExp(escapeCodeAsRegExpForMatchingInsideHTML(lines[line + 1]));
var src = findSourceInUrls(re, [url]);
item = {
Expand Down Expand Up @@ -1109,6 +1126,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
* @memberof TraceKit.computeStackTrace
*/
function computeStackTrace(ex, depth) {
curWin = TraceKit.windowPointer;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't seem to be used

var stack = null;
depth = (depth == null ? 0 : +depth);

Expand Down Expand Up @@ -1195,8 +1213,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