3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

Tolerate some Discord API errors before crashing

This commit is contained in:
Dragory 2019-04-23 05:29:53 +03:00
parent 0742c19cbb
commit 64dd1fc9e0

View file

@ -7,31 +7,38 @@ const fsp = fs.promises;
import { Knub, logger, PluginError, Plugin } from "knub";
import { SimpleError } from "./SimpleError";
import DiscordRESTError from "eris/lib/errors/DiscordRESTError"; // tslint:disable-line
import DiscordHTTPError from "eris/lib/errors/DiscordHTTPError"; // tslint:disable-line
require("dotenv").config();
let recentPluginErrors = 0;
const RECENT_ERROR_EXIT_THRESHOLD = 5;
const RECENT_PLUGIN_ERROR_EXIT_THRESHOLD = 5;
let recentDiscordErrors = 0;
const RECENT_DISCORD_ERROR_EXIT_THRESHOLD = 5;
setInterval(() => (recentPluginErrors = Math.max(0, recentPluginErrors - 1)), 2500);
setInterval(() => (recentDiscordErrors = Math.max(0, recentDiscordErrors - 1)), 2500);
process.on("unhandledRejection", (reason, p) => {
console.error(reason);
if (reason instanceof PluginError) {
if (++recentPluginErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1);
} else {
process.exit(1);
}
});
process.on("uncaughtException", err => {
function errorHandler(err) {
console.error(err);
if (err instanceof PluginError) {
if (++recentPluginErrors >= RECENT_ERROR_EXIT_THRESHOLD) process.exit(1);
// Tolerate a few recent plugin errors before crashing
if (++recentPluginErrors >= RECENT_PLUGIN_ERROR_EXIT_THRESHOLD) process.exit(1);
} else if (err instanceof DiscordRESTError || err instanceof DiscordHTTPError) {
// Discord API errors, usually safe to continue (rate limits etc. are handled elsewhere)
// We still bail if we get a ton of them in a short amount of time
if (++recentDiscordErrors >= RECENT_DISCORD_ERROR_EXIT_THRESHOLD) process.exit(1);
} else {
// On other errors, crash immediately
process.exit(1);
}
});
}
process.on("unhandledRejection", errorHandler);
process.on("uncaughtException", errorHandler);
// Verify required Node.js version
const REQUIRED_NODE_VERSION = "10.14.2";