Skip to content

Commit 67c726d

Browse files
authored
fix: Make sure that XHR is wrapped correctly for TryCatch and Instrument (#2343)
1 parent 66a1f02 commit 67c726d

File tree

4 files changed

+19
-16
lines changed

4 files changed

+19
-16
lines changed

packages/browser/src/integrations/trycatch.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,10 @@ export class TryCatch implements Integration {
164164
type: 'instrument',
165165
},
166166
};
167-
// If Instrument integration has been called before TryCatch
168-
// use it as "before" callback in the wrap and use the original instead
169-
if (original.__sentry__ && original.__sentry_original__) {
167+
168+
// If Instrument integration has been called before TryCatch, get the name of original function
169+
if (original.__sentry_original__) {
170170
wrapOptions.mechanism.data.handler = getFunctionName(original.__sentry_original__);
171-
return wrap(original.__sentry_original__, wrapOptions, original.__sentry_wrapped__);
172171
}
173172

174173
// Otherwise wrap directly

packages/browser/test/integration/suites/builtins.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,22 @@ describe("wrapped built-ins", function() {
389389
});
390390
});
391391

392+
it("should not call XMLHttpRequest onreadystatechange more than once", function() {
393+
return runInSandbox(sandbox, { manual: true }, function() {
394+
window.calls = 0;
395+
var xhr = new XMLHttpRequest();
396+
xhr.open("GET", "/base/subjects/example.json");
397+
xhr.onreadystatechange = function wat() {
398+
window.finalizeManualTest();
399+
window.calls += 1;
400+
};
401+
xhr.send();
402+
}).then(function(summary) {
403+
assert.equal(summary.window.calls, 3);
404+
delete summary.window.calls;
405+
});
406+
});
407+
392408
it(
393409
optional(
394410
"should capture built-in's mechanism type as instrument",

packages/utils/src/object.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,10 @@ export function fill(source: { [key: string]: any }, name: string, replacement:
2828
try {
2929
wrapped.prototype = wrapped.prototype || {};
3030
Object.defineProperties(wrapped, {
31-
__sentry__: {
32-
enumerable: false,
33-
value: true,
34-
},
3531
__sentry_original__: {
3632
enumerable: false,
3733
value: original,
3834
},
39-
__sentry_wrapped__: {
40-
enumerable: false,
41-
value: wrapped,
42-
},
4335
});
4436
} catch (_Oo) {
4537
// This can throw if multiple fill happens on a global object like XMLHttpRequest

packages/utils/test/object.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,9 @@ describe('fill()', () => {
6767
fill(source, name, replacement);
6868

6969
// Shouldn't show up in iteration
70-
expect(Object.keys(replacement)).not.toContain('__sentry__');
7170
expect(Object.keys(replacement)).not.toContain('__sentry_original__');
72-
expect(Object.keys(replacement)).not.toContain('__sentry_wrapped__');
7371
// But should be accessible directly
74-
expect(source.foo.__sentry__).toBe(true);
7572
expect(source.foo.__sentry_original__).toBe(source.foo);
76-
expect(source.foo.__sentry_wrapped__).toBe(source.foo);
7773
});
7874

7975
test('should preserve functions prototype if one exists', () => {

0 commit comments

Comments
 (0)