Skip to content

Commit b20d5ad

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

File tree

3 files changed

+51
-21
lines changed

3 files changed

+51
-21
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: 18 additions & 8 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);
@@ -78,7 +77,8 @@ TraceKit.report = (function reportModuleWrapper() {
7877
var handlers = [],
7978
lastArgs = null,
8079
lastException = null,
81-
lastExceptionStack = null;
80+
lastExceptionStack = null,
81+
hasDocument = document !== 'undefined';
8282

8383
/**
8484
* Add a crash handler.
@@ -133,6 +133,13 @@ TraceKit.report = (function reportModuleWrapper() {
133133
}
134134
}
135135

136+
function getLocationHref() {
137+
if (!hasDocument)
138+
return;
139+
140+
return getLocationHref();
141+
}
142+
136143
var _oldOnerrorHandler, _onErrorHandlerInstalled;
137144

138145
/**
@@ -168,7 +175,7 @@ TraceKit.report = (function reportModuleWrapper() {
168175
location.context = TraceKit.computeStackTrace.gatherContext(location.url, location.line);
169176
stack = {
170177
'message': message,
171-
'url': document.location.href,
178+
'url': getLocationHref(),
172179
'stack': [location]
173180
};
174181
notifyHandlers(stack, true);
@@ -512,6 +519,9 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
512519
* the url, line, and column number of the defined function.
513520
*/
514521
function findSourceByFunctionBody(func) {
522+
if (!hasDocument)
523+
return;
524+
515525
var urls = [window.location.href],
516526
scripts = document.getElementsByTagName('script'),
517527
body,
@@ -680,7 +690,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
680690
return {
681691
'name': ex.name,
682692
'message': ex.message,
683-
'url': document.location.href,
693+
'url': getLocationHref(),
684694
'stack': stack
685695
};
686696
}
@@ -737,7 +747,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
737747
return {
738748
'name': ex.name,
739749
'message': ex.message,
740-
'url': document.location.href,
750+
'url': getLocationHref(),
741751
'stack': stack
742752
};
743753
}
@@ -847,7 +857,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
847857
return {
848858
'name': ex.name,
849859
'message': lines[0],
850-
'url': document.location.href,
860+
'url': getLocationHref(),
851861
'stack': stack
852862
};
853863
}
@@ -984,7 +994,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
984994
var result = {
985995
'name': ex.name,
986996
'message': ex.message,
987-
'url': document.location.href,
997+
'url': getLocationHref(),
988998
'stack': stack
989999
};
9901000
augmentStackTraceWithInitialElement(result, ex.sourceURL || ex.fileName, ex.line || ex.lineNumber, ex.message || ex.description);
@@ -1050,7 +1060,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
10501060
return {
10511061
'name': ex.name,
10521062
'message': ex.message,
1053-
'url': document.location.href,
1063+
'url': getLocationHref(),
10541064
};
10551065
}
10561066

0 commit comments

Comments
 (0)