This repository has been archived on 2025-05-22. You can view files and clone it, but cannot push or open issues or pull requests.
mod-logs/src/utils/errorHandler.ts

36 lines
1.2 KiB
TypeScript

/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { EmbedBuilder, SnowflakeUtil } from "discord.js";
import { sendDebugLog } from "./sendDebugLog.js";
/**
* Parses an error event (re: from a catch block). If it
* is a proper Error object, extrapolates data. Sends information
* to the debug webhook, and assigns the error a Snowflake ID.
* @param context -- A brief description of the code module that threw the error.
* @param error -- The error payload, typed as unknown to comply with TypeScript's typedef.
* @returns The Snowflake ID assigned to the error.
*/
export const errorHandler = async(
context: string,
error: unknown,
): Promise<string> => {
const id = SnowflakeUtil.generate();
const embed = new EmbedBuilder();
embed.setFooter({ text: `Error ID: ${id.toString()}` });
embed.setTitle(`Error: ${context}`);
if (error instanceof Error) {
embed.setDescription(error.message);
embed.addFields([
{ name: "Stack", value: `\`\`\`\n${String(error.stack).slice(0, 1000)}` },
]);
} else {
embed.setDescription(String(error).slice(0, 2000));
}
await sendDebugLog({ embeds: [ embed ] });
return id.toString();
};