From 769ab0b90360c21c1d438b8cf07ec0340397ae57 Mon Sep 17 00:00:00 2001 From: Ben Vinegar Date: Tue, 15 Sep 2015 22:19:27 -0700 Subject: [PATCH] Don't access document if undefined --- src/raven.js | 7 ++++++- test/raven.test.js | 39 +++++++++++++++++++++++++------------ vendor/TraceKit/tracekit.js | 23 +++++++++++++++------- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/src/raven.js b/src/raven.js index a509bf9b2e52..88a7fa33636e 100644 --- a/src/raven.js +++ b/src/raven.js @@ -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, @@ -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); @@ -665,7 +670,7 @@ function now() { } function getHttpData() { - if (!document.location || !document.location.href) { + if (!hasDocument || !document.location || !document.location.href) { return; } diff --git a/test/raven.test.js b/test/raven.test.js index b5e91f8def4f..9d3b7b077804 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -1,5 +1,6 @@ function flushRavenState() { hasJSON = !isUndefined(window.JSON); + hasDocument = !isUndefined(document); lastCapturedException = undefined; lastEventId = undefined; globalServer = undefined; @@ -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); + }); }); }); diff --git a/vendor/TraceKit/tracekit.js b/vendor/TraceKit/tracekit.js index cc215d34b6c1..dd068cdf1188 100644 --- a/vendor/TraceKit/tracekit.js +++ b/vendor/TraceKit/tracekit.js @@ -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); @@ -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 * @@ -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); @@ -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, @@ -680,7 +689,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() { return { 'name': ex.name, 'message': ex.message, - 'url': document.location.href, + 'url': getLocationHref(), 'stack': stack }; } @@ -737,7 +746,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() { return { 'name': ex.name, 'message': ex.message, - 'url': document.location.href, + 'url': getLocationHref(), 'stack': stack }; } @@ -847,7 +856,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() { return { 'name': ex.name, 'message': lines[0], - 'url': document.location.href, + 'url': getLocationHref(), 'stack': stack }; } @@ -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); @@ -1050,7 +1059,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() { return { 'name': ex.name, 'message': ex.message, - 'url': document.location.href, + 'url': getLocationHref(), }; }