Skip to content

Commit 36935d9

Browse files
authored
fix: Multi fill + test (#2048)
* fix: Multi fill + test * meta: Changelog
1 parent cf24dd4 commit 36935d9

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- [browser] fix: Fixed a bug if Sentry was initialized multiple times: Fix #2043
56
- [browser] ref: Mangle more stuff, reduce bundle size
67
- [browser] fix: Support for ram bundle frames
78
- [node] fix: Expose lastEventId method

packages/browser/test/manual/npm-build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,10 @@ function runTests() {
5959
const scriptEl = window.document.createElement('script');
6060
scriptEl.textContent = myLibrary;
6161
window.document.body.appendChild(scriptEl);
62+
63+
// Testing https://github.com/getsentry/sentry-javascript/issues/2043
64+
const scriptEl2 = window.document.createElement('script');
65+
scriptEl2.textContent = myLibrary;
66+
window.document.body.appendChild(scriptEl2);
67+
// ------------------------------------------------------------------
6268
}

packages/browser/test/manual/test-code.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,9 @@ Sentry.withScope(scope => {
6262
Sentry.captureException(new TypeError('bar'));
6363
Sentry.captureMessage('baz');
6464
});
65+
66+
var xhr = new XMLHttpRequest();
67+
xhr.onload = () => console.log('loaded'); // This throws error
68+
// xhr.addEventListener("load", () => console.log('loaded')); This does not throw error
69+
xhr.open('GET', 'https://httpbin.org/get');
70+
xhr.send();

packages/utils/src/object.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,26 @@ export function fill(source: { [key: string]: any }, name: string, replacement:
2323
// otherwise it'll throw "TypeError: Object.defineProperties called on non-object"
2424
// tslint:disable-next-line:strict-type-predicates
2525
if (typeof wrapped === 'function') {
26-
wrapped.prototype = wrapped.prototype || {};
27-
Object.defineProperties(wrapped, {
28-
__sentry__: {
29-
enumerable: false,
30-
value: true,
31-
},
32-
__sentry_original__: {
33-
enumerable: false,
34-
value: original,
35-
},
36-
__sentry_wrapped__: {
37-
enumerable: false,
38-
value: wrapped,
39-
},
40-
});
26+
try {
27+
wrapped.prototype = wrapped.prototype || {};
28+
Object.defineProperties(wrapped, {
29+
__sentry__: {
30+
enumerable: false,
31+
value: true,
32+
},
33+
__sentry_original__: {
34+
enumerable: false,
35+
value: original,
36+
},
37+
__sentry_wrapped__: {
38+
enumerable: false,
39+
value: wrapped,
40+
},
41+
});
42+
} catch (_Oo) {
43+
// This can throw if multiple fill happens on a global object like XMLHttpRequest
44+
// Fixes https://github.com/getsentry/sentry-javascript/issues/2043
45+
}
4146
}
4247

4348
source[name] = wrapped;

packages/utils/test/object.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('fill()', () => {
3333
expect.assertions(3);
3434
});
3535

36-
test('mulitple fills calls all functions', () => {
36+
test('multiple fills calls all functions', () => {
3737
const source = {
3838
foo: (): number => 42,
3939
};

0 commit comments

Comments
 (0)