Skip to content

Commit

Permalink
Don't access document if undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Sep 16, 2015
1 parent 77b210b commit a59513e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// since JSON is required to encode the payload
var _Raven = window.Raven,
hasJSON = !!(typeof JSON === 'object' && JSON.stringify),
// Raven can run in contexts where there's no document (react-native)
hasDocument = isUndefined(document),
lastCapturedException,
lastEventId,
globalServer,
Expand Down Expand Up @@ -390,6 +392,9 @@ Raven.setUser = Raven.setUserContext; // To be deprecated
function triggerEvent(eventType, options) {
var event, key;

if (!hasDocument)
return;

options = options || {};

eventType = 'raven' + eventType.substr(0,1).toUpperCase() + eventType.substr(1);
Expand Down Expand Up @@ -665,7 +670,7 @@ function now() {
}

function getHttpData() {
if (!document.location || !document.location.href) {
if (!hasDocument || !document.location || !document.location.href) {
return;
}

Expand Down
39 changes: 27 additions & 12 deletions test/raven.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function flushRavenState() {
hasJSON = !isUndefined(window.JSON);
hasDocument = !isUndefined(document);
lastCapturedException = undefined;
lastEventId = undefined;
globalServer = undefined;
Expand Down Expand Up @@ -199,23 +200,37 @@ describe('globals', function() {
});

describe('getHttpData', function() {
var data = getHttpData();
var data;

it('should have a url', function() {
assert.equal(data.url, window.location.href);
before(function () {
data = getHttpData();
});

it('should have the user-agent header', function() {
assert.equal(data.headers['User-Agent'], navigator.userAgent);
describe('with document', function() {
it('should have a url', function() {
assert.equal(data.url, window.location.href);
});

it('should have the user-agent header', function() {
assert.equal(data.headers['User-Agent'], navigator.userAgent);
});

it('should have referer header when available', function() {
// lol this test is awful
if (window.document.referrer) {
assert.equal(data.headers.Referer, window.document.referrer);
} else {
assert.isUndefined(data.headers.Referer);
}
});
});

it('should have referer header when available', function() {
// lol this test is awful
if (window.document.referrer) {
assert.equal(data.headers.Referer, window.document.referrer);
} else {
assert.isUndefined(data.headers.Referer);
}
describe('without document', function () {
it('should return undefined if no document', function () {
hasDocument = false;
var data = getHttpData();
assert.isUndefined(data);
});
});
});

Expand Down
23 changes: 16 additions & 7 deletions vendor/TraceKit/tracekit.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var TraceKit = {
var _slice = [].slice;
var UNKNOWN_FUNCTION = '?';


/**
* TraceKit.wrap: Wrap any function in a TraceKit reporter
* Example: func = TraceKit.wrap(func);
Expand All @@ -35,6 +34,13 @@ TraceKit.wrap = function traceKitWrapper(func) {
return wrapped;
};

function getLocationHref() {
if (typeof document === 'undefined')
return '';

return document.location.href;
};

/**
* TraceKit.report: cross-browser processing of unhandled exceptions
*
Expand Down Expand Up @@ -168,7 +174,7 @@ TraceKit.report = (function reportModuleWrapper() {
location.context = TraceKit.computeStackTrace.gatherContext(location.url, location.line);
stack = {
'message': message,
'url': document.location.href,
'url': getLocationHref(),
'stack': [location]
};
notifyHandlers(stack, true);
Expand Down Expand Up @@ -512,6 +518,9 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
* the url, line, and column number of the defined function.
*/
function findSourceByFunctionBody(func) {
if (typeof document === 'undefined')
return;

var urls = [window.location.href],
scripts = document.getElementsByTagName('script'),
body,
Expand Down Expand Up @@ -680,7 +689,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
return {
'name': ex.name,
'message': ex.message,
'url': document.location.href,
'url': getLocationHref(),
'stack': stack
};
}
Expand Down Expand Up @@ -737,7 +746,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
return {
'name': ex.name,
'message': ex.message,
'url': document.location.href,
'url': getLocationHref(),
'stack': stack
};
}
Expand Down Expand Up @@ -847,7 +856,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
return {
'name': ex.name,
'message': lines[0],
'url': document.location.href,
'url': getLocationHref(),
'stack': stack
};
}
Expand Down Expand Up @@ -984,7 +993,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
var result = {
'name': ex.name,
'message': ex.message,
'url': document.location.href,
'url': getLocationHref(),
'stack': stack
};
augmentStackTraceWithInitialElement(result, ex.sourceURL || ex.fileName, ex.line || ex.lineNumber, ex.message || ex.description);
Expand Down Expand Up @@ -1050,7 +1059,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
return {
'name': ex.name,
'message': ex.message,
'url': document.location.href,
'url': getLocationHref(),
};
}

Expand Down

0 comments on commit a59513e

Please sign in to comment.