Skip to content

Commit 4464f31

Browse files
authored
feat(core): try to reduce bundle size caused by protocol modifications (#4301)
1 parent 0a0887e commit 4464f31

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

packages/core/src/integrations/inboundfilters.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,14 @@ export class InboundFilters implements Integration {
9999
}
100100

101101
try {
102-
return (
103-
(event &&
104-
event.exception &&
105-
event.exception.values &&
106-
event.exception.values[0] &&
107-
event.exception.values[0].type === 'SentryError') ||
108-
false
109-
);
110-
} catch (_oO) {
111-
return false;
102+
// @ts-ignore can't be a sentry error if undefined
103+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
104+
return event.exception.values[0].type === 'SentryError';
105+
} catch (e) {
106+
// ignore
112107
}
108+
109+
return false;
113110
}
114111

115112
/** JSDoc */
@@ -206,15 +203,16 @@ export class InboundFilters implements Integration {
206203
private _getEventFilterUrl(event: Event): string | null {
207204
try {
208205
if (event.stacktrace) {
209-
const frames = event.stacktrace.frames;
210-
return this._getLastValidUrl(frames);
206+
return this._getLastValidUrl(event.stacktrace.frames);
211207
}
212-
if (event.exception) {
213-
const frames =
214-
event.exception.values && event.exception.values[0].stacktrace && event.exception.values[0].stacktrace.frames;
215-
return this._getLastValidUrl(frames);
208+
let frames;
209+
try {
210+
// @ts-ignore we only care about frames if the whole thing here is defined
211+
frames = event.exception.values[0].stacktrace.frames;
212+
} catch (e) {
213+
// ignore
216214
}
217-
return null;
215+
return frames ? this._getLastValidUrl(frames) : null;
218216
} catch (oO) {
219217
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
220218
return null;

packages/utils/src/misc.ts

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Event, Mechanism, StackFrame } from '@sentry/types';
2+
import { Event, Exception, Mechanism, StackFrame } from '@sentry/types';
33

44
import { getGlobalObject } from './global';
55
import { snipLine } from './string';
@@ -90,23 +90,28 @@ export function parseUrl(
9090
};
9191
}
9292

93+
function getFirstException(event: Event): Exception | undefined {
94+
return event.exception && event.exception.values ? event.exception.values[0] : undefined;
95+
}
96+
9397
/**
9498
* Extracts either message or type+value from an event that can be used for user-facing logs
9599
* @returns event's description
96100
*/
97101
export function getEventDescription(event: Event): string {
98-
if (event.message) {
99-
return event.message;
102+
const { message, event_id: eventId } = event;
103+
if (message) {
104+
return message;
100105
}
101-
if (event.exception && event.exception.values && event.exception.values[0]) {
102-
const exception = event.exception.values[0];
103106

104-
if (exception.type && exception.value) {
105-
return `${exception.type}: ${exception.value}`;
107+
const firstException = getFirstException(event);
108+
if (firstException) {
109+
if (firstException.type && firstException.value) {
110+
return `${firstException.type}: ${firstException.value}`;
106111
}
107-
return exception.type || exception.value || event.event_id || '<unknown>';
112+
return firstException.type || firstException.value || eventId || '<unknown>';
108113
}
109-
return event.event_id || '<unknown>';
114+
return eventId || '<unknown>';
110115
}
111116

112117
/**
@@ -117,11 +122,15 @@ export function getEventDescription(event: Event): string {
117122
* @hidden
118123
*/
119124
export function addExceptionTypeValue(event: Event, value?: string, type?: string): void {
120-
event.exception = event.exception || {};
121-
event.exception.values = event.exception.values || [];
122-
event.exception.values[0] = event.exception.values[0] || {};
123-
event.exception.values[0].value = event.exception.values[0].value || value || '';
124-
event.exception.values[0].type = event.exception.values[0].type || type || 'Error';
125+
const exception = (event.exception = event.exception || {});
126+
const values = (exception.values = exception.values || []);
127+
const firstException = (values[0] = values[0] || {});
128+
if (!firstException.value) {
129+
firstException.value = value || '';
130+
}
131+
if (!firstException.type) {
132+
firstException.type = type || 'Error';
133+
}
125134
}
126135

127136
/**
@@ -132,18 +141,18 @@ export function addExceptionTypeValue(event: Event, value?: string, type?: strin
132141
* @hidden
133142
*/
134143
export function addExceptionMechanism(event: Event, newMechanism?: Partial<Mechanism>): void {
135-
if (!event.exception || !event.exception.values) {
144+
const firstException = getFirstException(event);
145+
if (!firstException) {
136146
return;
137147
}
138-
const exceptionValue0 = event.exception.values[0];
139148

140149
const defaultMechanism = { type: 'generic', handled: true };
141-
const currentMechanism = exceptionValue0.mechanism;
142-
exceptionValue0.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism };
150+
const currentMechanism = firstException.mechanism;
151+
firstException.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism };
143152

144153
if (newMechanism && 'data' in newMechanism) {
145154
const mergedData = { ...(currentMechanism && currentMechanism.data), ...newMechanism.data };
146-
exceptionValue0.mechanism.data = mergedData;
155+
firstException.mechanism.data = mergedData;
147156
}
148157
}
149158

0 commit comments

Comments
 (0)