Sending an error to the channel the error was caught on

I have a simple error handling script that checks for uncaughtExceptions, and if one pops up sends an error log both to the bot's console and to a dedicated error channel for internal use as an embed. If possible, I'd also like to send this embed to the user - for example, if a Slash Command fails, generate an error message and send it to the same channel the Slash Command was sent on. Is it possible to somehow get the sent interaction (and with it, the channel) that caused the error? I know I could encase my commands in a try-catch loop, but if possible, I'd rather not do that for every command I have.
export default (client: Client, instance: WOKCommands) => {
function generateGuruMeditation (errorType: string) {
// Just a string I can ask for to reference the error back,
// without asking the end user to take a screenshot.
return errorType + "-" + Math.floor(Date.now() / 1000);
}

function sendErrorToConsole (reason: any, parameter: any, guruMeditation: string) {
console.log("ā—" + reason + " (" + parameter + ")\nšŸ§˜: " + guruMeditation);
}

function sendErrorToChannel (reason: any, parameter: any, guruMeditation: string) {
// NOTE: This is an internal bot error channel.
// What I'd need is to also send this embed to the channel that made the error happen, if possible.
if (!process.env.ERROR_CHANNEL) return;
const channel = client.channels.cache.get(process.env.ERROR_CHANNEL);
if (!channel) return;

(channel as TextChannel).send({embeds: [createErrorEmbed(reason, parameter, guruMeditation)]});
}

function createErrorEmbed(reason: any, parameter: any, guruMeditation: string) {
return new MessageEmbed()
.setColor("RED")
.setTitle("āš ļø Error")
.setDescription("Looks like something went wrong!\nFeel free to try again - and if that doesn't help, ask in our [support server](" + process.env.SUPPORT_SERVER + ").\n\n" + reason + "\n\n" + parameter)
.setTimestamp()
.setFooter({text: ":person_in_lotus_position:: " + guruMeditation});
}

process.on("uncaughtException", (reason, parameter) => {
const guru = generateGuruMeditation("UNCEXC");
sendErrorToConsole(reason, parameter, guru);
sendErrorToChannel(reason, parameter, guru);
});

}
export default (client: Client, instance: WOKCommands) => {
function generateGuruMeditation (errorType: string) {
// Just a string I can ask for to reference the error back,
// without asking the end user to take a screenshot.
return errorType + "-" + Math.floor(Date.now() / 1000);
}

function sendErrorToConsole (reason: any, parameter: any, guruMeditation: string) {
console.log("ā—" + reason + " (" + parameter + ")\nšŸ§˜: " + guruMeditation);
}

function sendErrorToChannel (reason: any, parameter: any, guruMeditation: string) {
// NOTE: This is an internal bot error channel.
// What I'd need is to also send this embed to the channel that made the error happen, if possible.
if (!process.env.ERROR_CHANNEL) return;
const channel = client.channels.cache.get(process.env.ERROR_CHANNEL);
if (!channel) return;

(channel as TextChannel).send({embeds: [createErrorEmbed(reason, parameter, guruMeditation)]});
}

function createErrorEmbed(reason: any, parameter: any, guruMeditation: string) {
return new MessageEmbed()
.setColor("RED")
.setTitle("āš ļø Error")
.setDescription("Looks like something went wrong!\nFeel free to try again - and if that doesn't help, ask in our [support server](" + process.env.SUPPORT_SERVER + ").\n\n" + reason + "\n\n" + parameter)
.setTimestamp()
.setFooter({text: ":person_in_lotus_position:: " + guruMeditation});
}

process.on("uncaughtException", (reason, parameter) => {
const guru = generateGuruMeditation("UNCEXC");
sendErrorToConsole(reason, parameter, guru);
sendErrorToChannel(reason, parameter, guru);
});

}
4 Replies
d.js docs
d.js docsā€¢2y ago
ā€¢ What's your exact discord.js npm list discord.js and node node -v version? ā€¢ Post the full error stack trace, not just the top part! ā€¢ Show your code! ā€¢ Explain what exactly your issue is. ā€¢ Not a discord.js issue? Check out #useful-servers.
ShompiFlen
ShompiFlenā€¢2y ago
do you use an interaction command handler? you can use a try catch block there so you dont do it on all your commands, use async await, and basically if something fails it should be catched in the catch block where you have access to the interaction and the channel
monbrey
monbreyā€¢2y ago
This is largely why "simple" error handling like this isn't really the best approach. Catching individual errors at the source allows you to handle them properly
Honie
Honieā€¢2y ago
Yeah that makes sense! I use the premade WOKCommands library instead of an interaction handler built from scratch, so I might have to look into catch-ing using that. (If not creating my own command handler by myself, lol) For sure! This was my naive approach just looking up how other people do it, but it's not super useful if I actually want to return the error šŸ˜…
Want results from more Discord servers?
Add your server
More Posts