From ceb79a07e0a3d3289b461733ee0cc2b3b69ca95a Mon Sep 17 00:00:00 2001 From: Evorp <3vorpgaming@gmail.com> Date: Mon, 12 Feb 2024 18:29:30 -0800 Subject: [PATCH] fix axios stack traces and upgrade error handler --- functions/handleError.js | 29 ++++++++++++++++++++++++++++ functions/handleErrors.js | 40 --------------------------------------- index.js | 6 +++--- 3 files changed, 32 insertions(+), 43 deletions(-) create mode 100644 functions/handleError.js delete mode 100644 functions/handleErrors.js diff --git a/functions/handleError.js b/functions/handleError.js new file mode 100644 index 00000000..d040a140 --- /dev/null +++ b/functions/handleError.js @@ -0,0 +1,29 @@ +const devLogger = require("@helpers/devLogger"); +const { DiscordAPIError } = require("discord.js"); + +const DEV = process.env.DEV.toLowerCase() == "true"; + +/** + * Handle and log errors + * @author TheRolf, Evorp + * @param {import("discord.js").Client} client discord client + * @param {Error} error error description + * @param {string} type error title + */ +module.exports = function handleError(client, error, type) { + if (DEV) return console.error(error?.stack ?? error); + + const description = error.stack || JSON.stringify(error); + // if there's no stack, interpret the error as json + const codeBlocks = error.stack ? "" : "json"; + + if (error instanceof DiscordAPIError) + // not on our end + return console.error(error, type, description); + + // silence EPROTO errors + if (error.code == "EPROTO") return console.error(error, type, description); + + // DO NOT DELETE THIS CATCH, IT AVOIDS INFINITE LOOP IF THIS PROMISE REJECTS + devLogger(client, description, { title: type, codeBlocks }).catch(console.error); +}; diff --git a/functions/handleErrors.js b/functions/handleErrors.js deleted file mode 100644 index 5eb864c6..00000000 --- a/functions/handleErrors.js +++ /dev/null @@ -1,40 +0,0 @@ -const devLogger = require("@helpers/devLogger"); -const { DiscordAPIError } = require("discord.js"); - -const DEV = process.env.DEV.toLowerCase() == "true"; - -/** - * Handle errors and log them as necessary - * @author TheRolf, Evorp - * @param {import("discord.js").Client} client Discord client treating the information - * @param {Error} error the error itself - * @param {"Unhandled Rejection" | "Uncaught Exception"} type type of error - */ -module.exports = function handleErrors(client, error, type) { - if (DEV) return console.error(error?.stack ?? error); - - let eprotoError = false; - let description = error.stack; - let codeBlocks = ""; - - if (error.isAxiosError) { - // axios errors are JSON - description = JSON.stringify(error.toJSON()); - eprotoError = error.code === "EPROTO"; - codeBlocks = "json"; - } else if (!description) { - // no stack trace so it's JSON - description = JSON.stringify(error); - codeBlocks = "json"; - } else if (error instanceof DiscordAPIError) - // not on our end, just clutters logs - return console.error(error, description); - - // silence EPROTO errors - if (eprotoError) return console.error(error, description); - - console.error(error?.stack ?? error); - - // DO NOT DELETE THIS CATCH, IT AVOIDS INFINITE LOOP IF THIS PROMISE REJECTS - devLogger(client, description, { codeBlocks, title: type }).catch(console.error); -}; diff --git a/index.js b/index.js index aa1b38b9..e4657d4f 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ require("dotenv").config(); const { readdirSync } = require("fs"); const { fetchSettings } = require("@functions/fetchSettings"); -const handleErrors = require("@functions/handleErrors"); +const handleError = require("@functions/handleError"); const { Client, GatewayIntentBits, Partials } = require("discord.js"); function startBot() { @@ -64,8 +64,8 @@ function startBot() { /** * ERROR HANDLER */ - process.on("unhandledRejection", (reason) => handleErrors(client, reason, "Unhandled Rejection")); - process.on("uncaughtException", (error) => handleErrors(client, error, "Uncaught Exception")); + process.on("unhandledRejection", (reason) => handleError(client, reason, "Unhandled Rejection")); + process.on("uncaughtException", (error) => handleError(client, error, "Uncaught Exception")); client.login(process.env.CLIENT_TOKEN).catch(console.error); }