8000 fix: safeNormalize and serialize for Promise rejections (#1841) · productinfo/sentry-javascript@30909d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30909d5

Browse files
authored
fix: safeNormalize and serialize for Promise rejections (getsentry#1841)
* fix: safeNormalize and serialize * fix: Ensure max of 300 chars
1 parent 6c911dc commit 30909d5

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getCurrentHub } from '@sentry/core';
22
import { Integration, SentryEvent } from '@sentry/types';
33
import { logger } from '@sentry/utils/logger';
4+
import { safeNormalize, serialize } from '@sentry/utils/object';
5+
import { truncate } from '@sentry/utils/string';
46
import { addExceptionTypeValue, eventFromStacktrace } from '../parsers';
57
import {
68
installGlobalHandler,
@@ -110,7 +112,10 @@ export class GlobalHandlers implements Integration {
110112
},
111113
};
112114

113-
const fallbackValue = typeof stacktrace.original !== 'undefined' ? `${stacktrace.original}` : '';
115+
const fallbackValue =
116+
typeof stacktrace.original !== 'undefined'
117+
? `${truncate(serialize(safeNormalize(stacktrace.original)), 300)}`
118+
: '';
114119
const fallbackType = stacktrace.mechanism === 'onunhandledrejection' ? 'UnhandledRejection' : 'Error';
115120

116121
// This makes sure we have type/value in every exception

packages/browser/test/integration/test.js

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ for (var idx in frames) {
646646
);
647647
});
648648

649-
it('should capture unhandledrejection as string', function(done) {
649+
it('should capture unhandledrejection with a string', function(done) {
650650
var iframe = this.iframe;
651651

652652
iframeExecute(
@@ -661,7 +661,101 @@ for (var idx in frames) {
661661
},
662662
function(sentryData) {
663663
if (debounceAssertEventCount(sentryData, 1, done)) {
664-
assert.equal(sentryData[0].exception.values[0].value, 'test');
664+
assert.equal(sentryData[0].exception.values[0].value, '"test"');
665+
assert.equal(sentryData[0].exception.values[0].type, 'UnhandledRejection');
666+
assert.equal(sentryData[0].exception.values[0].stacktrace, undefined);
667+
assert.equal(sentryData[0].exception.mechanism.handled, false);
668+
assert.equal(sentryData[0].exception.mechanism.type, 'onunhandledrejection');
669+
done();
670+
} else {
671+
// This test will be skipped if it's not Chrome Desktop
672+
done();
673+
}
674+
}
675+
);
676+
});
677+
678+
it('should capture unhandledrejection with a monster string', function(done) {
679+
var iframe = this.iframe;
680+
681+
iframeExecute(
682+
iframe,
683+
done,
684+
function() {
685+
if (isChrome()) {
686+
Promise.reject('test'.repeat(100));
687+
} else {
688+
done();
689+
}
690+
},
691+
function(sentryData) {
692+
if (debounceAssertEventCount(sentryData, 1, done)) {
693+
assert.isAtMost(sentryData[0].exception.values[0].value.length, 303);
694+
assert.equal(sentryData[0].exception.values[0].type, 'UnhandledRejection');
695+
assert.equal(sentryData[0].exception.values[0].stacktrace, undefined);
696+
assert.equal(sentryData[0].exception.mechanism.handled, false);
697+
assert.equal(sentryData[0].exception.mechanism.type, 'onunhandledrejection');
698+
done();
699+
} else {
700+
// This test will be skipped if it's not Chrome Desktop
701+
done();
702+
}
703+
}
704+
);
705+
});
706+
707+
it('should capture unhandledrejection with an object', function(done) {
708+
var iframe = this.iframe;
709+
710+
iframeExecute(
711+
iframe,
712+
done,
713+
function() {
714+
if (isChrome()) {
715+
Promise.reject({ a: 'b' });
716+
} else {
717+
done();
718+
}
719+
},
720+
function(sentryData) {
721+
if (debounceAssertEventCount(sentryData, 1, done)) {
722+
assert.equal(sentryData[0].exception.values[0].value, '{"a":"b"}');
723+
assert.equal(sentryData[0].exception.values[0].type, 'UnhandledRejection');
724+
assert.equal(sentryData[0].exception.values[0].stacktrace, undefined);
725+
assert.equal(sentryData[0].exception.mechanism.handled, false);
726+
assert.equal(sentryData[0].exception.mechanism.type, 'onunhandledrejection');
727+
done();
728+
} else {
729+
// This test will be skipped if it's not Chrome Desktop
730+
done();
731+
}
732+
}
733+
);
734+
});
735+
736+
it('should capture unhandledrejection with an monster object', function(done) {
737+
var iframe = this.iframe;
738+
739+
iframeExecute(
740+
iframe,
741+
done,
742+
function() {
743+
if (isChrome()) {
744+
var a = {
745+
a: '1'.repeat('100'),
746+
b: '2'.repeat('100'),
747+
c: '3'.repeat('100'),
748+
};
749+
a.d = a.a;
750+
a.e = a;
751+
Promise.reject(a);
752+
} else {
753+
done();
754+
}
755+
28BE },
756+
function(sentryData) {
757+
if (debounceAssertEventCount(sentryData, 1, done)) {
758+
assert.isAtMost(sentryData[0].exception.values[0].value.length, 303);
665759
assert.equal(sentryData[0].exception.values[0].type, 'UnhandledRejection');
666760
assert.equal(sentryData[0].exception.values[0].stacktrace, undefined);
667761
assert.equal(sentryData[0].exception.mechanism.handled, false);

packages/utils/test/string.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ describe('truncate()', () => {
44
test('it works as expected', () => {
55
expect(truncate('lolol', 3)).toEqual('lol...');
66
expect(truncate('lolol', 10)).toEqual('lolol');
7-
expect(truncate('lol', 3)).toEqual('lol');
7+
expect(truncate('1'.repeat(1000), 300)).toHaveLength(303);
8+
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));
89
expect(truncate(new Array(1000).join('f'), 0)).toEqual(new Array(1000).join('f'));
910
});
1011

0 commit comments

Comments
 (0)
0