-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (115 loc) · 4.15 KB
/
Copy pathindex.js
File metadata and controls
131 lines (115 loc) · 4.15 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
const EXCEPTION_PATTERN = /([\w\d\.]*exception)\:\s*(.*)$/i;
const LINE_PATTERN = /((([\d\w]*\.)*[\d\w]*)\.)?([\d\w\$]*)\.([\d\w\$]*)\(([\d\w\.\s]*)(\:(\d*))?\)/;
export class StackPackage {
constructor(name, aliases = [], isVendor = false) {
if (!name || name.trim().length === 0) {
throw new Error('Cannot create stack package: name is not defined');
}
if (!aliases || !(aliases instanceof Array)) {
throw new Error('Cannot create stack package: aliases is not defined or of a wrong type');
}
this.name = name;
this.aliases = aliases;
this.isVendor = isVendor;
if (!String.prototype.startsWith) {
String.prototype.startsWith = function (search) {
return this.indexOf(search) === 0;
};
}
}
equals(stackPackage) {
if (!stackPackage) {
return false;
}
if (stackPackage.name === this.name) {
return true;
}
for (let i = 0; i < this.aliases.length; i++) {
if (this.aliases[i] === stackPackage.name || stackPackage.name.startsWith(this.aliases[i])) {
return true;
}
}
return false;
}
}
export class StackLine {
constructor(stackPackage, javaClass, method, source, line) {
if (!stackPackage || !(stackPackage instanceof StackPackage)) {
throw new Error('Cannot create stack line: stack package is null or of a wrong type');
}
this.stackPackage = stackPackage;
this.javaClass = javaClass || 'Unknown';
this.method = method || 'Unknown';
this.source = source || 'Unknown';
this.line = line ? parseInt(line, 10) : -1;
}
}
export class StackGroup {
constructor(stackPackage) {
if (!stackPackage || !(stackPackage instanceof StackPackage)) {
throw new Error('Cannot create stack group: stack package is null or of a wrong type');
}
this.stackPackage = stackPackage;
this.lines = [];
}
addLine(stackLine) {
if (stackLine instanceof StackLine) {
this.lines.push(stackLine);
}
}
addException(exception, message) {
this.exception = {
exception: exception,
message: message
};
}
}
export class Stack {
constructor(opts = {}) {
this.groups = [];
this.vendorPackages = [];
for (let key of Object.keys(opts)) {
this.vendorPackages.push(new StackPackage(key, opts[key], true));
}
}
parse(stackTrace) {
this.groups = [];
let parsedException = null;
let lines = stackTrace.split('\n');
lines.forEach((line)=> {
if (parsedException === null) {
parsedException = line.trim().match(EXCEPTION_PATTERN);
}
let parsedLine = line.trim().match(LINE_PATTERN);
if (parsedLine !== null) {
let stackPackage = new StackPackage(parsedLine[2] || 'Unknown');
for (let index in this.vendorPackages) {
if (this.vendorPackages[index].equals(stackPackage)) {
stackPackage = this.vendorPackages[index];
break;
}
}
let stackLine = new StackLine(
stackPackage,
parsedLine[4],
parsedLine[5],
parsedLine[6],
parsedLine[8]
);
if (this.groups.length === 0) {
this.groups.push(new StackGroup(stackPackage));
}
let group = this.groups[this.groups.length - 1];
if (!group.stackPackage.equals(stackPackage)) {
group = new StackGroup(stackPackage);
this.groups.push(group);
}
group.addLine(stackLine);
if (parsedException !== null) {
group.addException(parsedException[1], parsedException[2]);
parsedException = null;
}
}
});
}
}