-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathparser.ts
132 lines (102 loc) · 3.22 KB
/
parser.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
132
type SentryIssue = Record<string, any>;
export function getEvent(issue: SentryIssue) {
return issue?.event ?? issue?.data?.issue ?? issue;
}
export function getProject(issue: SentryIssue) {
return issue?.project?.project_name ?? getEvent(issue)?.project?.name;
}
export function getPlatform(issue: SentryIssue) {
return getEvent(issue)?.platform;
}
export function getLanguage(issue: SentryIssue) {
return getEvent(issue)?.location?.split(".")?.slice(-1)?.[0] || "";
}
export function getContexts(issue: SentryIssue) {
const contexts = getEvent(issue)?.contexts ?? {};
const values = Object.values(contexts)
.map((value: Record<string, unknown>) => `${value?.name} ${value?.version}`)
// TODO: Have a better decision tree here
.filter((value) => value !== "undefined undefined");
return values ?? [];
}
export function getExtras(issue: SentryIssue) {
const extras = getEvent(issue)?.extra ?? {};
const values = Object.entries(extras).map(
([key, value]) => `${key}: ${value}`
);
return values ?? [];
}
export function getLink(issue: SentryIssue) {
return issue?.url ?? "https://sentry.io";
}
export function getTags(issue: SentryIssue) {
return getEvent(issue)?.tags ?? [];
}
export function getLevel(issue: SentryIssue) {
return getEvent(issue)?.level;
}
export function getType(issue: SentryIssue) {
return getEvent(issue)?.type;
}
export function getTitle(issue: SentryIssue) {
return getEvent(issue)?.title ?? "Sentry Event";
}
export function getTime(issue: SentryIssue) {
const event = getEvent(issue);
if (event?.timestamp) {
return new Date(getEvent(issue)?.timestamp * 1000);
}
if (event?.lastSeen != null) {
return new Date(event?.lastSeen);
}
if (event?.firstSeen != null) {
return new Date(event?.firstSeen);
}
return new Date();
}
export function getRelease(issue: SentryIssue) {
return getEvent(issue)?.release;
}
export function getUser(issue: SentryIssue) {
return getEvent(issue)?.user;
}
export function getFileLocation(issue: SentryIssue) {
return getEvent(issue)?.location;
}
export function getStacktrace(issue: SentryIssue) {
return (
getEvent(issue)?.stacktrace ??
getEvent(issue)?.exception?.values[0]?.stacktrace
);
}
export function getErrorLocation(issue: SentryIssue, maxLines = Infinity) {
const stacktrace = getStacktrace(issue);
const locations = stacktrace?.frames; /*.reverse();*/
let files = locations?.map(
(location) =>
`${location?.filename}, ${location?.lineno ?? "?"}:${
location?.colno ?? "?"
}`
);
if (maxLines < Infinity && files?.length > maxLines) {
files = files.slice(0, maxLines);
files.push("...");
}
return files;
}
export function getErrorCodeSnippet(issue: SentryIssue) {
const stacktrace = getStacktrace(issue);
const location = stacktrace?.frames?.reverse()?.[0];
if (!location) {
const event = getEvent(issue);
return event?.culprit ?? null;
}
// The spaces below are intentional - they help align the code
// aorund the additional `>` marker
return ` ${location.pre_context?.join("\n ") ?? ""}\n>${
location.context_line
}\n${location.post_context?.join("\n") ?? ""}`;
}
export function getMessage(issue: SentryIssue) {
return issue?.message;
}