|
1 |
| -import { getCurrentHub } from '@sentry/hub'; |
2 |
| -import { Event, EventHint, Mechanism, Options, Severity } from '@sentry/types'; |
3 |
| -import { |
4 |
| - addExceptionMechanism, |
5 |
| - addExceptionTypeValue, |
6 |
| - extractExceptionKeysForMessage, |
7 |
| - isError, |
8 |
| - isPlainObject, |
9 |
| - normalizeToSize, |
10 |
| -} from '@sentry/utils'; |
11 |
| - |
12 |
| -import { extractStackFromError, parseError, parseStack, prepareFramesForEvent } from './parsers'; |
13 |
| - |
14 |
| -/** |
15 |
| - * Builds and Event from a Exception |
16 |
| - * @hidden |
17 |
| - */ |
18 |
| -export function eventFromException(exception: unknown, hint?: EventHint): Event { |
19 |
| - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
20 |
| - let ex: any = exception; |
21 |
| - const providedMechanism: Mechanism | undefined = |
22 |
| - hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism; |
23 |
| - const mechanism: Mechanism = providedMechanism || { |
24 |
| - handled: true, |
25 |
| - type: 'generic', |
26 |
| - }; |
27 |
| - |
28 |
| - if (!isError(exception)) { |
29 |
| - if (isPlainObject(exception)) { |
30 |
| - // This will allow us to group events based on top-level keys |
31 |
| - // which is much better than creating new group when any key/value change |
32 |
| - const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`; |
33 |
| - |
34 |
| - getCurrentHub().configureScope(scope => { |
35 |
| - scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>)); |
36 |
| - }); |
37 |
| - |
38 |
| - ex = (hint && hint.syntheticException) || new Error(message); |
39 |
| - (ex as Error).message = message; |
40 |
| - } else { |
41 |
| - // This handles when someone does: `throw "something awesome";` |
42 |
| - // We use synthesized Error here so we can extract a (rough) stack trace. |
43 |
| - ex = (hint && hint.syntheticException) || new Error(exception as string); |
44 |
| - (ex as Error).message = exception as string; |
45 |
| - } |
46 |
| - mechanism.synthetic = true; |
47 |
| - } |
48 |
| - |
49 |
| - const event = parseError(ex as Error); |
50 |
| - addExceptionTypeValue(event, undefined, undefined); |
51 |
| - addExceptionMechanism(event, mechanism); |
52 |
| - |
53 |
| - return { |
54 |
| - ...event, |
55 |
| - event_id: hint && hint.event_id, |
56 |
| - }; |
57 |
| -} |
58 |
| - |
59 |
| -/** |
60 |
| - * Builds and Event from a Message |
61 |
| - * @hidden |
62 |
| - */ |
63 |
| -export function eventFromMessage( |
64 |
| - options: Options, |
65 |
| - message: string, |
66 |
| - level: Severity = Severity.Info, |
67 |
| - hint?: EventHint, |
68 |
| -): Event { |
69 |
| - const event: Event = { |
70 |
| - event_id: hint && hint.event_id, |
71 |
| - level, |
72 |
| - message, |
73 |
| - }; |
74 |
| - |
75 |
| - if (options.attachStacktrace && hint && hint.syntheticException) { |
76 |
| - const stack = hint.syntheticException ? extractStackFromError(hint.syntheticException) : []; |
77 |
| - const frames = parseStack(stack); |
78 |
| - |
79 |
| - event.stacktrace = { |
80 |
| - frames: prepareFramesForEvent(frames), |
81 |
| - }; |
82 |
| - } |
83 |
| - |
84 |
| - return event; |
85 |
| -} |
| 1 | +import { getCurrentHub } from '@sentry/hub'; |
| 2 | +import { Event, EventHint, Mechanism, Options, Severity } from '@sentry/types'; |
| 3 | +import { |
| 4 | + addExceptionMechanism, |
| 5 | + addExceptionTypeValue, |
| 6 | + extractExceptionKeysForMessage, |
| 7 | + isError, |
| 8 | + isPlainObject, |
| 9 | + normalizeToSize, |
| 10 | +} from '@sentry/utils'; |
| 11 | + |
| 12 | +import { extractStackFromError, parseError, parseStack, prepareFramesForEvent } from './parsers'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Builds and Event from a Exception |
| 16 | + * @hidden |
| 17 | + */ |
| 18 | +export function eventFromException(exception: unknown, hint?: EventHint): Event { |
| 19 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 20 | + let ex: any = exception; |
| 21 | + const providedMechanism: Mechanism | undefined = |
| 22 | + hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism; |
| 23 | + const mechanism: Mechanism = providedMechanism || { |
| 24 | + handled: true, |
| 25 | + type: 'generic', |
| 26 | + }; |
| 27 | + |
| 28 | + if (!isError(exception)) { |
| 29 | + if (isPlainObject(exception)) { |
| 30 | + // This will allow us to group events based on top-level keys |
| 31 | + // which is much better than creating new group when any key/value change |
| 32 | + const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`; |
| 33 | + |
| 34 | + getCurrentHub().configureScope(scope => { |
| 35 | + scope.setExtra('__serialized__', normalizeToSize(exception as Record<string, unknown>)); |
| 36 | + }); |
| 37 | + |
| 38 | + ex = (hint && hint.syntheticException) || new Error(message); |
| 39 | + (ex as Error).message = message; |
| 40 | + } else { |
| 41 | + // This handles when someone does: `throw "something awesome";` |
| 42 | + // We use synthesized Error here so we can extract a (rough) stack trace. |
| 43 | + ex = (hint && hint.syntheticException) || new Error(exception as string); |
| 44 | + (ex as Error).message = exception as string; |
| 45 | + } |
| 46 | + mechanism.synthetic = true; |
| 47 | + } |
| 48 | + |
| 49 | + const event = parseError(ex as Error); |
| 50 | + addExceptionTypeValue(event, undefined, undefined); |
| 51 | + addExceptionMechanism(event, mechanism); |
| 52 | + |
| 53 | + return { |
| 54 | + ...event, |
| 55 | + event_id: hint && hint.event_id, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Builds and Event from a Message |
| 61 | + * @hidden |
| 62 | + */ |
| 63 | +export function eventFromMessage( |
| 64 | + options: Options, |
| 65 | + message: string, |
| 66 | + level: Severity = Severity.Info, |
| 67 | + hint?: EventHint, |
| 68 | +): Event { |
| 69 | + const event: Event = { |
| 70 | + event_id: hint && hint.event_id, |
| 71 | + level, |
| 72 | + message, |
| 73 | + }; |
| 74 | + |
| 75 | + if (options.attachStacktrace && hint && hint.syntheticException) { |
| 76 | + const stack = hint.syntheticException ? extractStackFromError(hint.syntheticException) : []; |
| 77 | + const frames = parseStack(stack); |
| 78 | + |
| 79 | + event.stacktrace = { |
| 80 | + frames: prepareFramesForEvent(frames), |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return event; |
| 85 | +} |
0 commit comments