Skip to content

Commit 39c6a5b

Browse files
committed
Wrap xhr prototype methods on open, not send
1 parent 8f6ba80 commit 39c6a5b

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

src/raven.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,10 @@ Raven.prototype = {
606606
}
607607
});
608608

609-
var origSend;
609+
var origOpen;
610610
if ('XMLHttpRequest' in window) {
611-
origSend = XMLHttpRequest.prototype.send;
612-
XMLHttpRequest.prototype.send = function (data) { // preserve arity
611+
origOpen = XMLHttpRequest.prototype.open;
612+
XMLHttpRequest.prototype.open = function (data) { // preserve arity
613613
var xhr = this;
614614
'onreadystatechange onload onerror onprogress'.replace(/\w+/g, function (prop) {
615615
if (prop in xhr && Object.prototype.toString.call(xhr[prop]) === '[object Function]') {
@@ -618,7 +618,7 @@ Raven.prototype = {
618618
});
619619
}
620620
});
621-
origSend.apply(this, arguments);
621+
origOpen.apply(this, arguments);
622622
};
623623
}
624624

test/integration/example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}

test/integration/frame.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<script>
88
Raven._makeRequest = function () {};
99

10+
window.origSetTimeout = setTimeout;
1011
Raven.config('https://[email protected]/1', {
1112
dataCallback: function (data) {
1213
window.ravenData = data;

test/integration/test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function iframeExecute(iframe, done, execute, assertCallback) {
99
}
1010
}
1111
// use setTimeout so stack trace doesn't go all the way back to mocha test runner
12-
iframe.contentWindow.eval('setTimeout(' + execute.toString() + ');');
12+
iframe.contentWindow.eval('origSetTimeout(' + execute.toString() + ');');
1313
}
1414

1515
function createIframe(done) {
@@ -136,7 +136,7 @@ describe('integration', function () {
136136
},
137137
function () {
138138
var ravenData = iframe.contentWindow.ravenData;
139-
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, eventHandlerStackDepth + 2);
139+
assert.equal(ravenData.exception.values[0].stacktrace.frames.length, eventHandlerStackDepth + 1);
140140
}
141141
);
142142
});
@@ -223,5 +223,27 @@ describe('integration', function () {
223223
}
224224
);
225225
});
226+
227+
it('should capture exceptions from XMLHttpRequest event handlers (e.g. onreadystatechange)', function (done) {
228+
var iframe = this.iframe;
229+
230+
iframeExecute(iframe, done,
231+
function () {
232+
setTimeout(done);
233+
var xhr = new XMLHttpRequest();
234+
xhr.onreadystatechange = function () {
235+
foo();
236+
}
237+
xhr.open('GET', 'example.json');
238+
xhr.send();
239+
},
240+
function () {
241+
var ravenData = iframe.contentWindow.ravenData;
242+
console.log(ravenData);
243+
// # of frames alter significantly between chrome/firefox & safari
244+
assert.isTrue(ravenData.exception.values[0].stacktrace.frames.length >= 4);
245+
}
246+
);
247+
});
226248
});
227249
});

0 commit comments

Comments
 (0)