Skip to content

Don't access document if undefined #383

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

Merged
merged 1 commit into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we maybe use window.location.href instead? And avoid the document issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly ... but it wouldn't surprise me if location is similarly undefined. I'll check.

};

/**
* 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