Skip to content

Commit c9e33d4

Browse files
committed
Don't access document if undefined
1 parent 77b210b commit c9e33d4

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

src/raven.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// since JSON is required to encode the payload
66
var _Raven = window.Raven,
77
hasJSON = !!(typeof JSON === 'object' && JSON.stringify),
8+
// Raven can run in contexts where there's no document (react-native)
9+
hasDocument = typeof document !== 'undefined',
810
lastCapturedException,
911
lastEventId,
1012
globalServer,
@@ -390,6 +392,9 @@ Raven.setUser = Raven.setUserContext; // To be deprecated
390392
function triggerEvent(eventType, options) {
391393
var event, key;
392394

395+
if (!hasDocument)
396+
return;
397+
393398
options = options || {};
394399

395400
eventType = 'raven' + eventType.substr(0,1).toUpperCase() + eventType.substr(1);
@@ -665,7 +670,7 @@ function now() {
665670
}
666671

667672
function getHttpData() {
668-
if (!document.location || !document.location.href) {
673+
if (!hasDocument || !document.location || !document.location.href) {
669674
return;
670675
}
671676

test/raven.test.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function flushRavenState() {
22
hasJSON = !isUndefined(window.JSON);
3+
hasDocument = !isUndefined(document);
34
lastCapturedException = undefined;
45
lastEventId = undefined;
56
globalServer = undefined;
@@ -199,23 +200,37 @@ describe('globals', function() {
199200
});
200201

201202
describe('getHttpData', function() {
202-
var data = getHttpData();
203+
var data;
203204

204-
it('should have a url', function() {
205-
assert.equal(data.url, window.location.href);
205+
before(function () {
206+
data = getHttpData();
206207
});
207208

208-
it('should have the user-agent header', function() {
209-
assert.equal(data.headers['User-Agent'], navigator.userAgent);
209+
describe('with document', function() {
210+
it('should have a url', function() {
211+
assert.equal(data.url, window.location.href);
212+
});
213+
214+
it('should have the user-agent header', function() {
215+
assert.equal(data.headers['User-Agent'], navigator.userAgent);
216+
});
217+
218+
it('should have referer header when available', function() {
219+
// lol this test is awful
220+
if (window.document.referrer) {
221+
assert.equal(data.headers.Referer, window.document.referrer);
222+
} else {
223+
assert.isUndefined(data.headers.Referer);
224+
}
225+
});
210226
});
211227

212-
it('should have referer header when available', function() {
213-
// lol this test is awful
214-
if (window.document.referrer) {
215-
assert.equal(data.headers.Referer, window.document.referrer);
216-
} else {
217-
assert.isUndefined(data.headers.Referer);
218-
}
228+
describe('without document', function () {
229+
it('should return undefined if no document', function () {
230+
hasDocument = false;
231+
var data = getHttpData();
232+
assert.isUndefined(data);
233+
});
219234
});
220235
});
221236

vendor/TraceKit/tracekit.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ var TraceKit = {
1515
var _slice = [].slice;
1616
var UNKNOWN_FUNCTION = '?';
1717

18-
1918
/**
2019
* TraceKit.wrap: Wrap any function in a TraceKit reporter
2120
* Example: func = TraceKit.wrap(func);
@@ -35,6 +34,13 @@ TraceKit.wrap = function traceKitWrapper(func) {
3534
return wrapped;
3635
};
3736

37+
TraceKit.getLocationHref = function () {
38+
if (typeof document === 'undefined')
39+
return '';
40+
41+
return document.location.href;
42+
};
43+
3844
/**
3945
* TraceKit.report: cross-browser processing of unhandled exceptions
4046
*
@@ -168,7 +174,7 @@ TraceKit.report = (function reportModuleWrapper() {
168174
location.context = TraceKit.computeStackTrace.gatherContext(location.url, location.line);
169175
stack = {
170176
'message': message,
171-
'url': document.location.href,
177+
'url': TraceKit.getLocationHref(),
172178
'stack': [location]
173179
};
174180
notifyHandlers(stack, true);
@@ -512,6 +518,9 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
512518
* the url, line, and column number of the defined function.
513519
*/
514520
function findSourceByFunctionBody(func) {
521+
if (typeof document === 'undefined')
522+
return;
523+
515524
var urls = [window.location.href],
516525
scripts = document.getElementsByTagName('script'),
517526
body,
@@ -680,7 +689,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
680689
return {
681690
'name': ex.name,
682691
'message': ex.message,
683-
'url': document.location.href,
692+
'url': TraceKit.getLocationHref(),
684693
'stack': stack
685694
};
686695
}
@@ -737,7 +746,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
737746
return {
738747
'name': ex.name,
739748
'message': ex.message,
740-
'url': document.location.href,
749+
'url': TraceKit.getLocationHref(),
741750
'stack': stack
742751
};
743752
}
@@ -847,7 +856,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
847856
return {
848857
'name': ex.name,
849858
'message': lines[0],
850-
'url': document.location.href,
859+
'url': TraceKit.getLocationHref(),
851860
'stack': stack
852861
};
853862
}
@@ -984,7 +993,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
984993
var result = {
985994
'name': ex.name,
986995
'message': ex.message,
987-
'url': document.location.href,
996+
'url': TraceKit.getLocationHref(),
988997
'stack': stack
989998
};
990999
augmentStackTraceWithInitialElement(result, ex.sourceURL || ex.fileName, ex.line || ex.lineNumber, ex.message || ex.description);
@@ -1050,7 +1059,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
10501059
return {
10511060
'name': ex.name,
10521061
'message': ex.message,
1053-
'url': document.location.href,
1062+
'url': TraceKit.getLocationHref(),
10541063
};
10551064
}
10561065

0 commit comments

Comments
 (0)