-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (78 loc) · 2.92 KB
/
index.js
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
const { EventEmitter } = require('events');
const colors = require('colors');
const axios = require('axios');
class NoCrasher extends EventEmitter {
constructor(options) {
super();
this.options = options;
this.checkOptions();
if (this.options.enableNoCrasher) {
this.setupEventListeners();
console.log('[nocrasher] : [nocrasher] is now preventing crashes.'.green);
} else {
console.log('[nocrasher] : Option enableNoCrasher is disabled, and [nocrasher] will not be preventing bot termination'.red);
}
if (this.options.enableWebhook) {
this.setupWebhook();
}
}
checkOptions() {
if (!this.options || typeof this.options.enableNoCrasher !== 'boolean') {
console.error('[nocrasher] : Option enableNoCrasher is missing or has an invalid value! Check the documentation.'.red);
}
if (this.options.enableWebhook) {
if (typeof this.options.enableWebhook !== 'boolean') {
console.error('[nocrasher] : Option enableWebhook must be a boolean! Check the documentation.'.red);
} else if (typeof this.options.webhookURL !== 'string') {
console.error('[nocrasher] : Option webhookURL must be a string when enableWebhook is true! Check the documentation.'.red);
}
}
}
setupEventListeners() {
process.on('unhandledRejection', (reason, p) => {
this.logError('Unhandled Rejection/Catch', reason, p);
});
process.on('uncaughtException', (err, origin) => {
this.logError('Uncaught Exception/Catch', err, origin);
});
process.on('uncaughtExceptionMonitor', (err, origin) => {
this.logError('Uncaught Exception/Catch (Monitor)', err, origin);
});
process.on('multipleResolves', (type, promise, reason) => {
//this.logError('Multiple Resolves', type, promise, reason);
});
}
setupWebhook() {
if (typeof this.options.webhookURL === 'string') {
this.webhookURL = this.options.webhookURL;
} else {
console.error('[nocrasher] : Option webhookURL must be provided as a string when enableWebhook is true! Check the documentation.'.red);
}
}
logError(type, ...args) {
console.error(`[nocrasher] : ${type}`.red);
console.error(...args);
if (this.options.enableWebhook && this.webhookURL) {
this.sendToWebhook(type, args);
}
}
sendToWebhook(type, errors) {
const errorMessage = errors.map(error => this.stringifyError(error)).join('\n');
if (errorMessage.length > 4000) {
message = message.slice(0, 4000) + '...';
}
const data = {
content: `**Error Type:** ${type}\n\`\`\`${errorMessage}\`\`\``
};
axios.post(this.webhookURL, data)
.catch((error) => console.error('[nocrasher] : Failed to send error to webhook.', error));
}
stringifyError(error) {
if (error instanceof Error) {
return `${error.name}: ${error.message}\n${error.stack}`;
} else {
return error.toString();
}
}
}
module.exports = NoCrasher;