forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventExclusionPlugin.ts
More file actions
127 lines (112 loc) · 3.4 KB
/
EventExclusionPlugin.ts
File metadata and controls
127 lines (112 loc) · 3.4 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
import { InnerErrorInfo } from "../../models/data/ErrorInfo.js";
import { isMatch, startsWith, toBoolean } from "../../Utils.js";
import { EventPluginContext } from "../EventPluginContext.js";
import { IEventPlugin } from "../IEventPlugin.js";
export class EventExclusionPlugin implements IEventPlugin {
public priority: number = 45;
public name: string = "EventExclusionPlugin";
public run(context: EventPluginContext): Promise<void> {
const ev = context.event;
const log = context.log;
const settings = context.client.config.settings;
if (ev.type === "log") {
const minLogLevel = this.getMinLogLevel(settings, ev.source);
const logLevel = this.getLogLevel(ev.data["@level"]);
if (logLevel !== -1 && (logLevel === 6 || logLevel < minLogLevel)) {
log.info("Cancelling log event due to minimum log level.");
context.cancelled = true;
}
} else if (ev.type === "error") {
let error: InnerErrorInfo = ev.data["@error"];
while (!context.cancelled && error) {
if (
this.getTypeAndSourceSetting(settings, ev.type, error.type, true) ===
false
) {
log.info(
`Cancelling error from excluded exception type: ${error.type}`,
);
context.cancelled = true;
}
error = error.inner;
}
} else if (
this.getTypeAndSourceSetting(settings, ev.type, ev.source, true) === false
) {
log.info(
`Cancelling event from excluded type: ${ev.type} and source: ${ev.source}`,
);
context.cancelled = true;
}
return Promise.resolve();
}
public getLogLevel(level: string): number {
switch ((level || "").toLowerCase().trim()) {
case "trace":
case "true":
case "1":
case "yes":
return 0;
case "debug":
return 1;
case "info":
return 2;
case "warn":
return 3;
case "error":
return 4;
case "fatal":
return 5;
case "off":
case "false":
case "0":
case "no":
return 6;
default:
return -1;
}
}
public getMinLogLevel(
configSettings: Record<string, string>,
source,
): number {
return this.getLogLevel(
this.getTypeAndSourceSetting(configSettings, "log", source, "other") + "",
);
}
private getTypeAndSourceSetting(
configSettings: Record<string, string> = {},
type: string,
source: string,
defaultValue: string | boolean,
): string | boolean {
if (!type) {
return defaultValue;
}
if (!source) {
source = "";
}
const isLog: boolean = type === "log";
const sourcePrefix: string = `@@${type}:`;
const value: string = configSettings[sourcePrefix + source];
if (value) {
return isLog ? value : toBoolean(value);
}
// sort object keys longest first, then alphabetically.
const sortedKeys = Object.keys(configSettings).sort((a, b) =>
b.length - a.length || a.localeCompare(b)
);
for (const index in sortedKeys) {
const key: string = sortedKeys[index];
if (!startsWith(key.toLowerCase(), sourcePrefix)) {
continue;
}
// check for wildcard match
const cleanKey: string = key.substring(sourcePrefix.length);
if (isMatch(source, [cleanKey])) {
return isLog ? configSettings[key] : toBoolean(configSettings[key]);
}
}
return defaultValue;
}
}