Skip to content

Commit

Permalink
ref: simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jun 22, 2023
1 parent abfd72f commit 3857456
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sentryTest('should capture an POJO', async ({ getLocalTestPath, page }) => {
expect(eventData.exception?.values).toHaveLength(1);
expect(eventData.exception?.values?.[0]).toMatchObject({
type: 'Error',
value: '`MyTestClass` captured as exception with keys: prop1, prop2',
value: 'Object captured as exception with keys: prop1, prop2',
mechanism: {
type: 'generic',
handled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ sentryTest('should capture an Event', async ({ getLocalTestPath, page }) => {
expect(eventData.exception?.values).toHaveLength(1);
expect(eventData.exception?.values?.[0]).toMatchObject({
type: 'Event',
value: 'Event `Event` (custom) captured as exception with keys: currentTarget, isTrusted, target, type',
value: 'Event `Event` (type=custom) captured as exception',
mechanism: {
type: 'generic',
handled: true,
Expand Down
14 changes: 5 additions & 9 deletions packages/browser/src/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,23 +290,19 @@ function getNonErrorObjectExceptionValue(
): string {
const keys = extractExceptionKeysForMessage(exception);
const captureType = isUnhandledRejection ? 'promise rejection' : 'exception';
const className = getObjectClassName(exception);

// Some ErrorEvent instances do not have an `error` property, which is why they are not handled before
// We still want to try to get a decent message for these cases
if (isErrorEvent(exception)) {
return `Event \`ErrorEvent\` captured as ${captureType} with message \`${exception.message}\``;
}

const name = isEvent(exception)
? `Event \`${className}\` (${exception.type})`
: className && className !== 'Object'
? `\`${className}\``
: 'Object';

const label = `${name} captured as ${captureType}`;
if (isEvent(exception)) {
const className = getObjectClassName(exception);
return `Event \`${className}\` (type=${exception.type}) captured as ${captureType}`;
}

return `${label} with keys: ${keys}`;
return `Object captured as ${captureType} with keys: ${keys}`;
}

function getObjectClassName(obj: unknown): string | undefined | void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('window.onunhandledrejection', function () {
// non-error rejections don't provide stacktraces so we can skip that assertion
assert.equal(
summary.events[0].exception.values[0].value,
'Event `Event` (unhandledrejection) captured as promise rejection with keys: currentTarget, isTrusted, target, type'
'Event `Event` (type=unhandledrejection) captured as promise rejection'
);
assert.equal(summary.events[0].exception.values[0].type, 'Event');
assert.equal(summary.events[0].exception.values[0].mechanism.handled, false);
Expand Down
14 changes: 3 additions & 11 deletions packages/browser/test/unit/eventbuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,9 @@ describe('eventFromPlainObject', () => {
it.each([
['empty object', {}, 'Object captured as exception with keys: [object has no keys]'],
['pojo', { prop1: 'hello', prop2: 2 }, 'Object captured as exception with keys: prop1, prop2'],
['Custom Class', new MyTestClass(), '`MyTestClass` captured as exception with keys: prop1, prop2'],
[
'Event',
new Event('custom'),
'Event `Event` (custom) captured as exception with keys: currentTarget, isTrusted, target, type',
],
[
'MouseEvent',
new MouseEvent('click'),
'Event `MouseEvent` (click) captured as exception with keys: currentTarget, isTrusted, target, type',
],
['Custom Class', new MyTestClass(), 'Object captured as exception with keys: prop1, prop2'],
['Event', new Event('custom'), 'Event `Event` (type=custom) captured as exception'],
['MouseEvent', new MouseEvent('click'), 'Event `MouseEvent` (type=click) captured as exception'],
] as [string, Record<string, unknown>, string][])(
'has correct exception value for %s',
(_name, exception, expected) => {
Expand Down

0 comments on commit 3857456

Please sign in to comment.