Skip to content

feat(core): try to reduce bundle size caused by protocol modifications #4301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions packages/core/src/integrations/inboundfilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,14 @@ export class InboundFilters implements Integration {
}

try {
return (
(event &&
event.exception &&
event.exception.values &&
event.exception.values[0] &&
event.exception.values[0].type === 'SentryError') ||
false
);
} catch (_oO) {
return false;
// @ts-ignore can't be a sentry error if undefined
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
return event.exception.values[0].type === 'SentryError';
} catch (e) {
// ignore
}

return false;
}

/** JSDoc */
Expand Down Expand Up @@ -206,15 +203,16 @@ export class InboundFilters implements Integration {
private _getEventFilterUrl(event: Event): string | null {
try {
if (event.stacktrace) {
const frames = event.stacktrace.frames;
return this._getLastValidUrl(frames);
return this._getLastValidUrl(event.stacktrace.frames);
}
if (event.exception) {
const frames =
event.exception.values && event.exception.values[0].stacktrace && event.exception.values[0].stacktrace.frames;
return this._getLastValidUrl(frames);
let frames;
try {
// @ts-ignore we only care about frames if the whole thing here is defined
frames = event.exception.values[0].stacktrace.frames;
} catch (e) {
// ignore
}
return null;
return frames ? this._getLastValidUrl(frames) : null;
} catch (oO) {
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
return null;
Expand Down
47 changes: 28 additions & 19 deletions packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Event, Mechanism, StackFrame } from '@sentry/types';
import { Event, Exception, Mechanism, StackFrame } from '@sentry/types';

import { getGlobalObject } from './global';
import { snipLine } from './string';
Expand Down Expand Up @@ -90,23 +90,28 @@ export function parseUrl(
};
}

function getFirstException(event: Event): Exception | undefined {
return event.exception && event.exception.values ? event.exception.values[0] : undefined;
}

/**
* Extracts either message or type+value from an event that can be used for user-facing logs
* @returns event's description
*/
export function getEventDescription(event: Event): string {
if (event.message) {
return event.message;
const { message, event_id: eventId } = event;
if (message) {
return message;
}
if (event.exception && event.exception.values && event.exception.values[0]) {
const exception = event.exception.values[0];

if (exception.type && exception.value) {
return `${exception.type}: ${exception.value}`;
const firstException = getFirstException(event);
if (firstException) {
if (firstException.type && firstException.value) {
return `${firstException.type}: ${firstException.value}`;
}
return exception.type || exception.value || event.event_id || '<unknown>';
return firstException.type || firstException.value || eventId || '<unknown>';
}
return event.event_id || '<unknown>';
return eventId || '<unknown>';
}

/**
Expand All @@ -117,11 +122,15 @@ export function getEventDescription(event: Event): string {
* @hidden
*/
export function addExceptionTypeValue(event: Event, value?: string, type?: string): void {
event.exception = event.exception || {};
event.exception.values = event.exception.values || [];
event.exception.values[0] = event.exception.values[0] || {};
event.exception.values[0].value = event.exception.values[0].value || value || '';
event.exception.values[0].type = event.exception.values[0].type || type || 'Error';
const exception = (event.exception = event.exception || {});
const values = (exception.values = exception.values || []);
const firstException = (values[0] = values[0] || {});
if (!firstException.value) {
firstException.value = value || '';
}
if (!firstException.type) {
firstException.type = type || 'Error';
}
}

/**
Expand All @@ -132,18 +141,18 @@ export function addExceptionTypeValue(event: Event, value?: string, type?: strin
* @hidden
*/
export function addExceptionMechanism(event: Event, newMechanism?: Partial<Mechanism>): void {
if (!event.exception || !event.exception.values) {
const firstException = getFirstException(event);
if (!firstException) {
return;
}
const exceptionValue0 = event.exception.values[0];

const defaultMechanism = { type: 'generic', handled: true };
const currentMechanism = exceptionValue0.mechanism;
exceptionValue0.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism };
const currentMechanism = firstException.mechanism;
firstException.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism };

if (newMechanism && 'data' in newMechanism) {
const mergedData = { ...(currentMechanism && currentMechanism.data), ...newMechanism.data };
exceptionValue0.mechanism.data = mergedData;
firstException.mechanism.data = mergedData;
}
}

Expand Down