-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
eventbuilder.ts
131 lines (117 loc) · 3.43 KB
/
eventbuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { getCurrentHub } from '@sentry/hub';
import {
Event,
EventHint,
Exception,
Mechanism,
Severity,
SeverityLevel,
StackFrame,
StackParser,
} from '@sentry/types';
import {
addExceptionMechanism,
addExceptionTypeValue,
extractExceptionKeysForMessage,
isError,
isPlainObject,
normalizeToSize,
} from '@sentry/utils';
/**
* Extracts stack frames from the error.stack string
*/
export function parseStackFrames(stackParser: StackParser, error: Error): StackFrame[] {
return stackParser(error.stack || '', 1);
}
/**
* Extracts stack frames from the error and builds a Sentry Exception
*/
export function exceptionFromError(stackParser: StackParser, error: Error): Exception {
const exception: Exception = {
type: error.name || error.constructor.name,
value: error.message,
};
const frames = parseStackFrames(stackParser, error);
if (frames.length) {
exception.stacktrace = { frames };
}
return exception;
}
/**
* Builds and Event from a Exception
* @hidden
*/
export function eventFromUnknownInput(stackParser: StackParser, exception: unknown, hint?: EventHint): Event {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let ex: unknown = exception;
const providedMechanism: Mechanism | undefined =
hint && hint.data && (hint.data as { mechanism: Mechanism }).mechanism;
const mechanism: Mechanism = providedMechanism || {
handled: true,
type: 'generic',
};
if (!isError(exception)) {
if (isPlainObject(exception)) {
// This will allow us to group events based on top-level keys
// which is much better than creating new group when any key/value change
const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;
const hub = getCurrentHub();
const client = hub.getClient();
const normalizeDepth = client && client.getOptions().normalizeDepth;
hub.configureScope(scope => {
scope.setExtra('__serialized__', normalizeToSize(exception, normalizeDepth));
});
ex = (hint && hint.syntheticException) || new Error(message);
(ex as Error).message = message;
} else {
// This handles when someone does: `throw "something awesome";`
// We use synthesized Error here so we can extract a (rough) stack trace.
ex = (hint && hint.syntheticException) || new Error(exception as string);
(ex as Error).message = exception as string;
}
mechanism.synthetic = true;
}
const event = {
exception: {
values: [exceptionFromError(stackParser, ex as Error)],
},
};
addExceptionTypeValue(event, undefined, undefined);
addExceptionMechanism(event, mechanism);
return {
...event,
event_id: hint && hint.event_id,
};
}
/**
* Builds and Event from a Message
* @hidden
*/
export function eventFromMessage(
stackParser: StackParser,
message: string,
// eslint-disable-next-line deprecation/deprecation
level: Severity | SeverityLevel = 'info',
hint?: EventHint,
attachStacktrace?: boolean,
): Event {
const event: Event = {
event_id: hint && hint.event_id,
level,
message,
};
if (attachStacktrace && hint && hint.syntheticException) {
const frames = parseStackFrames(stackParser, hint.syntheticException);
if (frames.length) {
event.exception = {
values: [
{
value: message,
stacktrace: { frames },
},
],
};
}
}
return event;
}