Skip to content

Commit

Permalink
fix axios stack traces and upgrade error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Feb 13, 2024
1 parent 6e7b8d5 commit ceb79a0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
29 changes: 29 additions & 0 deletions functions/handleError.js
Original file line number Diff line number Diff line change
@@ -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);
};
40 changes: 0 additions & 40 deletions functions/handleErrors.js

This file was deleted.

6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit ceb79a0

Please sign in to comment.