-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy patheventbuilder.ts
More file actions
173 lines (151 loc) · 4.6 KB
/
Copy patheventbuilder.ts
File metadata and controls
173 lines (151 loc) · 4.6 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import type {
Client,
Event,
EventHint,
Exception,
Extras,
Hub,
Mechanism,
ParameterizedString,
Severity,
SeverityLevel,
StackFrame,
StackParser,
} from '@sentry/types';
import { isError, isParameterizedString, isPlainObject } from './is';
import { addExceptionMechanism, addExceptionTypeValue } from './misc';
import { normalizeToSize } from './normalize';
import { extractExceptionKeysForMessage } from './object';
/**
* 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;
}
function getMessageForObject(exception: object): string {
if ('name' in exception && typeof exception.name === 'string') {
let message = `'${exception.name}' captured as exception`;
if ('message' in exception && typeof exception.message === 'string') {
message += ` with message '${exception.message}'`;
}
return message;
} else if ('message' in exception && typeof exception.message === 'string') {
return exception.message;
} else {
// 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
return `Object captured as exception with keys: ${extractExceptionKeysForMessage(
exception as Record<string, unknown>,
)}`;
}
}
/**
* Builds and Event from a Exception
*
* TODO(v8): Remove getHub fallback
* @hidden
*/
export function eventFromUnknownInput(
// eslint-disable-next-line deprecation/deprecation
getHubOrClient: (() => Hub) | Client | undefined,
stackParser: StackParser,
exception: unknown,
hint?: EventHint,
): Event {
const client =
typeof getHubOrClient === 'function'
? // eslint-disable-next-line deprecation/deprecation
getHubOrClient().getClient()
: getHubOrClient;
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',
};
let extras: Extras | undefined;
if (!isError(exception)) {
if (isPlainObject(exception)) {
const normalizeDepth = client && client.getOptions().normalizeDepth;
extras = { ['__serialized__']: normalizeToSize(exception as Record<string, unknown>, normalizeDepth) };
const message = getMessageForObject(exception);
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: Event = {
exception: {
values: [exceptionFromError(stackParser, ex as Error)],
},
};
if (extras) {
event.extra = extras;
}
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: ParameterizedString,
// 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,
};
if (attachStacktrace && hint && hint.syntheticException) {
const frames = parseStackFrames(stackParser, hint.syntheticException);
if (frames.length) {
event.exception = {
values: [
{
value: message,
stacktrace: { frames },
},
],
};
}
}
if (isParameterizedString(message)) {
const { __sentry_template_string__, __sentry_template_values__ } = message;
event.logentry = {
message: __sentry_template_string__,
params: __sentry_template_values__,
};
return event;
}
event.message = message;
return event;
}