From da919d1c5a749a9f013245c9617e024bef128f3a Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Thu, 31 Aug 2023 21:46:58 +0200 Subject: [PATCH] fix: Updated handling of interaction errors --- src/handlers/interactionErrorHandler.ts | 46 ++++++++++++++----------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/handlers/interactionErrorHandler.ts b/src/handlers/interactionErrorHandler.ts index 852e2d68..6ce4a3c2 100644 --- a/src/handlers/interactionErrorHandler.ts +++ b/src/handlers/interactionErrorHandler.ts @@ -4,6 +4,7 @@ import { EmbedBuilder, Interaction, InteractionReplyOptions, + InteractionType, MessageComponentInteraction } from 'discord.js'; import { Logger } from 'pino'; @@ -39,48 +40,53 @@ export const handleError = async ( ] }; - logger.error({ error, interaction }, `Error handling interaction '${interactionIdentifier}'`); + logger.debug(error, `Error handling interaction '${interactionIdentifier}' with execution ID ${executionId}`); - if (interaction instanceof ChatInputCommandInteraction && interaction.deferred) { + if (interaction instanceof ChatInputCommandInteraction) { switch (interaction.replied) { case true: - logger.warn(error, `Interaction '${interaction.id}' threw an error but has already been replied to.`); + logger.warn( + error, + `Interaction '${interactionIdentifier}' threw an error but has already been replied to.` + ); return; case false: logger.debug('Responding with error embed'); return await interaction.editReply(errorReply); } - } else if (interaction instanceof MessageComponentInteraction && interaction.deferred) { + } else if (interaction instanceof MessageComponentInteraction) { switch (interaction.replied) { case true: - logger.warn(error, `Interaction '${interaction.id}' threw an error but has already been replied to.`); + logger.warn( + error, + `Interaction '${interactionIdentifier}' threw an error but has already been replied to.` + ); return; case false: logger.debug('Responding with error embed'); return await interaction.editReply(errorReply); } } else { - logger.warn( - 'Interaction threw an error but was not deferred or replied to, or was an autocomplete interaction. Cannot send error reply.' + logger.debug( + `${ + InteractionType[interaction.type] + } interaction '${interactionIdentifier}' threw an error. Cannot send error reply.` ); - if ( - error.code === 'InteractionCollectorError' || - error.message === 'Collector received no interactions before ending with reason: time' - ) { - logger.debug('Interaction response timed out.'); + if (error.message === 'Unknown interaction') { + logger.debug('Interaction no longer exists, timed out or has already been responded to.'); return; } - if (error.message === 'Unknown interaction') { - logger.debug('Interaction has already been responded to or does no longer exist.'); + if (error.message === 'Collector received no interactions before ending with reason: time') { + logger.debug('Interaction collector response timed out.'); return; } - - logger.fatal( - error, - `Unhandled error while awaiting or handling component interaction. Execution ID: ${executionId}` - ); - return; } + + logger.fatal( + error, + `Unhandled error while handling interaction '${interactionIdentifier}'. Execution ID: ${executionId}` + ); + return; };