mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 15:00:00 +00:00
Handle eris errors in common error handler
This commit is contained in:
parent
3538d6c66a
commit
1a3d6d2fd9
2 changed files with 78 additions and 49 deletions
14
backend/src/ErisError.ts
Normal file
14
backend/src/ErisError.ts
Normal file
|
@ -0,0 +1,14 @@
|
|||
export class ErisError extends Error {
|
||||
code: number | string | undefined;
|
||||
shardId: number;
|
||||
|
||||
constructor(message: string, code: number | string | undefined, shardId: number) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.shardId = shardId;
|
||||
}
|
||||
|
||||
toString() {
|
||||
return `[ERIS] [CODE ${this.code || "?"}] [SHARD ${this.shardId}] ${this.message}`;
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ import { LogType } from "./data/LogType";
|
|||
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
||||
import { logger } from "./logger";
|
||||
import { PluginLoadError } from "knub/dist/plugins/PluginLoadError";
|
||||
import { ErisError } from "./ErisError";
|
||||
|
||||
const fsp = fs.promises;
|
||||
|
||||
|
@ -51,8 +52,14 @@ const RECENT_DISCORD_ERROR_EXIT_THRESHOLD = 5;
|
|||
setInterval(() => (recentPluginErrors = Math.max(0, recentPluginErrors - 1)), 2000);
|
||||
setInterval(() => (recentDiscordErrors = Math.max(0, recentDiscordErrors - 1)), 2000);
|
||||
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
const errorHandler = err => {
|
||||
// Eris handles these internally, so we don't need to panic if we get one of them
|
||||
const SAFE_TO_IGNORE_ERIS_ERROR_CODES = [
|
||||
1001, // "CloudFlare WebSocket proxy restarting"
|
||||
1006, // "Connection reset by peer"
|
||||
"ECONNRESET", // Pretty much the same as above
|
||||
];
|
||||
|
||||
function errorHandler(err) {
|
||||
const guildName = err.guild?.name || "Global";
|
||||
const guildId = err.guild?.id || "0";
|
||||
|
||||
|
@ -77,8 +84,15 @@ if (process.env.NODE_ENV === "production") {
|
|||
return;
|
||||
}
|
||||
|
||||
if (err instanceof ErisError) {
|
||||
if (err.code && SAFE_TO_IGNORE_ERIS_ERROR_CODES.includes(err.code)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (err instanceof DiscordHTTPError && err.code === 500) {
|
||||
// Don't need stack traces on HTTP 500 errors
|
||||
// These also shouldn't count towards RECENT_DISCORD_ERROR_EXIT_THRESHOLD because they don't indicate an error in our code
|
||||
console.error(err.message);
|
||||
return;
|
||||
}
|
||||
|
@ -104,8 +118,9 @@ if (process.env.NODE_ENV === "production") {
|
|||
process.exit(1);
|
||||
}
|
||||
// tslint:enable:no-console
|
||||
};
|
||||
}
|
||||
|
||||
if (process.env.NODE_ENV === "production") {
|
||||
process.on("uncaughtException", errorHandler);
|
||||
process.on("unhandledRejection", errorHandler);
|
||||
}
|
||||
|
@ -154,8 +169,8 @@ connect().then(async () => {
|
|||
}
|
||||
});
|
||||
|
||||
client.on("error", err => {
|
||||
logger.error(`[ERIS] ${String(err)}`);
|
||||
client.on("error", (err, shardId) => {
|
||||
errorHandler(new ErisError(err.message, (err as any).code, shardId));
|
||||
});
|
||||
|
||||
const allowedGuilds = new AllowedGuilds();
|
||||
|
|
Loading…
Add table
Reference in a new issue