Skip to content

Commit 804f7a7

Browse files
authored
ref(core): Streamline module_metadata assignment and cleanup functions (#17696)
Quick PR to streamline the stack frame `module_metadata` property assignment (`addMetadataToStackFrames`) and cleanup (`stripMetadataFromStackFrames`) logic, since we can use optional chaining now without a bundle size hit from polyfilling. Originally went with moving the cleanup logic into the client but decided against it due to the bundle size hit (see comment).
1 parent 97ff2f7 commit 804f7a7

File tree

4 files changed

+12
-31
lines changed

4 files changed

+12
-31
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export { functionToStringIntegration } from './integrations/functiontostring';
101101
export { inboundFiltersIntegration } from './integrations/eventFilters';
102102
export { eventFiltersIntegration } from './integrations/eventFilters';
103103
export { linkedErrorsIntegration } from './integrations/linkederrors';
104-
export { moduleMetadataIntegration } from './integrations/metadata';
104+
export { moduleMetadataIntegration } from './integrations/moduleMetadata';
105105
export { requestDataIntegration } from './integrations/requestdata';
106106
export { captureConsoleIntegration } from './integrations/captureconsole';
107107
export { dedupeIntegration } from './integrations/dedupe';
File renamed without changes.

packages/core/src/integrations/third-party-errors-filter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export const thirdPartyErrorFilterIntegration = defineIntegration((options: Opti
4242
name: 'ThirdPartyErrorsFilter',
4343
setup(client) {
4444
// We need to strip metadata from stack frames before sending them to Sentry since these are client side only.
45-
// TODO(lforst): Move this cleanup logic into a more central place in the SDK.
4645
client.on('beforeEnvelope', envelope => {
4746
forEachEnvelopeItem(envelope, (item, type) => {
4847
if (type === 'event') {

packages/core/src/metadata.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,46 +53,28 @@ export function getMetadataForUrl(parser: StackParser, filename: string): any |
5353
* Metadata is injected by the Sentry bundler plugins using the `_experiments.moduleMetadata` config option.
5454
*/
5555
export function addMetadataToStackFrames(parser: StackParser, event: Event): void {
56-
try {
57-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
58-
event.exception!.values!.forEach(exception => {
59-
if (!exception.stacktrace) {
56+
event.exception?.values?.forEach(exception => {
57+
exception.stacktrace?.frames?.forEach(frame => {
58+
if (!frame.filename || frame.module_metadata) {
6059
return;
6160
}
6261

63-
for (const frame of exception.stacktrace.frames || []) {
64-
if (!frame.filename || frame.module_metadata) {
65-
continue;
66-
}
62+
const metadata = getMetadataForUrl(parser, frame.filename);
6763

68-
const metadata = getMetadataForUrl(parser, frame.filename);
69-
70-
if (metadata) {
71-
frame.module_metadata = metadata;
72-
}
64+
if (metadata) {
65+
frame.module_metadata = metadata;
7366
}
7467
});
75-
} catch {
76-
// To save bundle size we're just try catching here instead of checking for the existence of all the different objects.
77-
}
68+
});
7869
}
7970

8071
/**
8172
* Strips metadata from stack frames.
8273
*/
8374
export function stripMetadataFromStackFrames(event: Event): void {
84-
try {
85-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
86-
event.exception!.values!.forEach(exception => {
87-
if (!exception.stacktrace) {
88-
return;
89-
}
90-
91-
for (const frame of exception.stacktrace.frames || []) {
92-
delete frame.module_metadata;
93-
}
75+
event.exception?.values?.forEach(exception => {
76+
exception.stacktrace?.frames?.forEach(frame => {
77+
delete frame.module_metadata;
9478
});
95-
} catch {
96-
// To save bundle size we're just try catching here instead of checking for the existence of all the different objects.
97-
}
79+
});
9880
}

0 commit comments

Comments
 (0)