Skip to content

Commit e97cf36

Browse files
committed
use actual Mechanisms in addExceptionMechanism
1 parent e60b87b commit e97cf36

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

packages/utils/src/misc.ts

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Event, Integration, StackFrame, WrappedFunction } from '@sentry/types';
2+
import { Event, Integration, Mechanism, StackFrame, WrappedFunction } from '@sentry/types';
33

44
import { isNodeEnv } from './node';
55
import { snipLine } from './string';
@@ -197,30 +197,21 @@ export function addExceptionTypeValue(event: Event, value?: string, type?: strin
197197
}
198198

199199
/**
200-
* Adds exception mechanism to a given event.
200+
* Adds exception mechanism data to a given event.
201201
* @param event The event to modify.
202-
* @param mechanism Mechanism of the mechanism.
202+
* @param newMechanism Mechanism data to add to the event.
203203
* @hidden
204204
*/
205-
export function addExceptionMechanism(
206-
event: Event,
207-
mechanism: {
208-
[key: string]: any;
209-
} = {},
210-
): void {
211-
// TODO: Use real type with `keyof Mechanism` thingy and maybe make it better?
212-
try {
213-
// @ts-ignore Type 'Mechanism | {}' is not assignable to type 'Mechanism | undefined'
214-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
215-
event.exception!.values![0].mechanism = event.exception!.values![0].mechanism || {};
216-
Object.keys(mechanism).forEach(key => {
217-
// @ts-ignore Mechanism has no index signature
218-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
219-
event.exception!.values![0].mechanism[key] = mechanism[key];
220-
});
221-
} catch (_oO) {
222-
// no-empty
205+
export function addExceptionMechanism(event: Event, newMechanism: Partial<Mechanism>): void {
206+
if (!event.exception || !event.exception.values) {
207+
return;
223208
}
209+
const exceptionValue0 = event.exception.values[0];
210+
211+
const defaultMechanism = { type: 'generic', handled: true };
212+
const currentMechanism = exceptionValue0.mechanism;
213+
const mergedData = { ...currentMechanism?.data, ...newMechanism.data };
214+
exceptionValue0.mechanism = { ...defaultMechanism, ...currentMechanism, ...newMechanism, data: mergedData };
224215
}
225216

226217
/**

0 commit comments

Comments
 (0)