Skip to content

Commit e054f1f

Browse files
committed
feat: Use useful event description instead of event_id for user-facing logs
1 parent 4ff4f09 commit e054f1f

File tree

4 files changed

+139
-7
lines changed

4 files changed

+139
-7
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { API, getCurrentHub, logger } from '@sentry/core';
22
import { Integration, Severity } from '@sentry/types';
33
import { isFunction, isString } from '@sentry/utils/is';
44
import { getGlobalObject, parseUrl } from '@sentry/utils/misc';
5+
import { getEventDescription } from '@sentry/utils/misc';
56
import { deserialize, fill } from '@sentry/utils/object';
67
import { safeJoin } from '@sentry/utils/string';
78
import { supportsBeacon, supportsHistory, supportsNativeFetch } from '@sentry/utils/supports';
@@ -26,14 +27,13 @@ function addSentryBreadcrumb(serializedData: string): void {
2627
// There's always something that can go wrong with deserialization...
2728
try {
2829
const event: { [key: string]: any } = deserialize(serializedData);
29-
const exception = event.exception && event.exception.values && event.exception.values[0];
3030

3131
getCurrentHub().addBreadcrumb(
3232
{
3333
category: 'sentry',
3434
event_id: event.event_id,
3535
level: event.level || Severity.fromString('error'),
36-
message: exception ? `${exception.type ? `${exception.type}: ` : ''}${exception.value}` : event.message,
36+
message: getEventDescription(event),
3737
},
3838
{
3939
event,

packages/browser/src/integrations/inboundfilters.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { configureScope, logger } from '@sentry/core';
22
import { Integration, SentryEvent } from '@sentry/types';
33
import { isRegExp } from '@sentry/utils/is';
44
import { BrowserOptions } from '../backend';
5+
import { getEventDescription } from '@sentry/utils/misc';
56

67
// "Script error." is hard coded into browsers for errors that it can't read.
78
// this is the result of a script being pulled in from an external domain and CORS.
@@ -39,15 +40,15 @@ export class InboundFilters implements Integration {
3940
/** JSDoc */
4041
public shouldDropEvent(event: SentryEvent): boolean {
4142
if (this.isIgnoredError(event)) {
42-
logger.warn(`Event dropped due to being matched by \`ignoreErrors\` option.\n Event: ${event.event_id}`);
43+
logger.warn(`Event dropped due to being matched by \`ignoreErrors\` option.\n Event: ${getEventDescription(event)}`);
4344
return true;
4445
}
4546
if (this.isBlacklistedUrl(event)) {
46-
logger.warn(`Event dropped due to being matched by \`blacklistUrls\` option.\n Event: ${event.event_id}`);
47+
logger.warn(`Event dropped due to being matched by \`blacklistUrls\` option.\n Event: ${getEventDescription(event)}`);
4748
return true;
4849
}
4950
if (!this.isWhitelistedUrl(event)) {
50-
logger.warn(`Event dropped due to not being matched by \`whitelistUrls\` option.\n Event: ${event.event_id}`);
51+
logger.warn(`Event dropped due to not being matched by \`whitelistUrls\` option.\n Event: ${getEventDescription(event)}`);
5152
return true;
5253
}
5354
return false;
@@ -120,7 +121,7 @@ export class InboundFilters implements Integration {
120121
const { type, value } = evt.exception.values[0];
121122
return [`${value}`, `${type}: ${value}`];
122123
} catch (oO) {
123-
logger.error(`Cannot extract message for event ${event.event_id}`);
124+
logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
124125
return [];
125126
}
126127
} else {
@@ -141,7 +142,7 @@ export class InboundFilters implements Integration {
141142
return '';
142143
}
143144
} catch (oO) {
144-
logger.error(`Cannot extract url for event ${event.event_id}`);
145+
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
145146
return '';
146147
}
147148
}

packages/utils/src/misc.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SentryEvent } from '@sentry/types';
12
import { isString } from './is';
23

34
/**
@@ -179,3 +180,23 @@ export function parseUrl(
179180
relative: match[5] + query + fragment, // everything minus origin
180181
};
181182
}
183+
184+
/**
185+
* Extracts either message or type+value from an event that can be used for user-facing logs
186+
* @returns event's description
187+
*/
188+
export function getEventDescription(event: SentryEvent): string {
189+
if (event.message) {
190+
return event.message;
191+
} else if (event.exception && event.exception.values && event.exception.values[0]) {
192+
const exception = event.exception.values[0];
193+
194+
if (exception.type && exception.value) {
195+
return `${exception.type}: ${exception.value}`;
196+
} else {
197+
return exception.type || exception.value || event.event_id || '<unknown>';
198+
}
199+
} else {
200+
return event.event_id || '<unknown>';
201+
}
202+
}

packages/utils/test/misc.test.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { getEventDescription } from '../src/misc';
2+
3+
describe('getEventDescription()', () => {
4+
test('message event', () => {
5+
expect(
6+
getEventDescription({
7+
message: 'Random message',
8+
exception: {
9+
values: [
10+
{
11+
type: 'SyntaxError',
12+
value: 'wat',
13+
},
14+
],
15+
},
16+
}),
17+
).toEqual('Random message');
18+
});
19+
20+
test('exception event with just type', () => {
21+
expect(
22+
getEventDescription({
23+
exception: {
24+
values: [
25+
{
26+
type: 'SyntaxError',
27+
},
28+
],
29+
},
30+
}),
31+
).toEqual('SyntaxError');
32+
});
33+
34+
test('exception event with just value', () => {
35+
expect(
36+
getEventDescription({
37+
exception: {
38+
values: [
39+
{
40+
value: 'wat',
41+
},
42+
],
43+
},
44+
}),
45+
).toEqual('wat');
46+
});
47+
48+
test('exception event with type and value', () => {
49+
expect(
50+
getEventDescription({
51+
exception: {
52+
values: [
53+
{
54+
type: 'SyntaxError',
55+
value: 'wat',
56+
},
57+
],
58+
},
59+
}),
60+
).toEqual('SyntaxError: wat');
61+
});
62+
63+
test('exception event with invalid type and value, but with event_id', () => {
64+
expect(
65+
getEventDescription({
66+
exception: {
67+
values: [
68+
{
69+
type: undefined,
70+
value: undefined,
71+
},
72+
],
73+
},
74+
event_id: '123',
75+
}),
76+
).toEqual('123');
77+
});
78+
79+
test('exception event with invalid type and value and no event_id', () => {
80+
expect(
81+
getEventDescription({
82+
exception: {
83+
values: [
84+
{
85+
type: undefined,
86+
value: undefined,
87+
},
88+
],
89+
},
90+
}),
91+
).toEqual('<unknown>');
92+
});
93+
94+
test('malformed event with just event_id', () => {
95+
expect(
96+
getEventDescription({
97+
event_id: '123',
98+
}),
99+
).toEqual('123');
100+
});
101+
102+
test('completely malformed event', () => {
103+
expect(
104+
getEventDescription({
105+
oh: 'come, on',
106+
really: '?',
107+
} as any),
108+
).toEqual('<unknown>');
109+
});
110+
});

0 commit comments

Comments
 (0)