From e89701e3f9007ce6c095017edc68da7b934a2c0c Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Thu, 16 Nov 2023 21:10:24 +0200 Subject: [PATCH] Provide more context in exception messages --- .../Infra/ExportWrapper.cs | 4 +-- .../Commands/Base/ExportCommandBase.cs | 4 ++- .../DiscordChatExporterException.cs | 4 +-- .../Exporting/ChannelExporter.cs | 36 +++++++++++++------ .../Components/DashboardViewModel.cs | 4 +-- 5 files changed, 33 insertions(+), 19 deletions(-) diff --git a/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs b/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs index 1302fb2..44c48ab 100644 --- a/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs +++ b/DiscordChatExporter.Cli.Tests/Infra/ExportWrapper.cs @@ -105,7 +105,7 @@ public static class ExportWrapper if (message is null) { throw new InvalidOperationException( - $"Message '{messageId}' does not exist in the export of channel '{channelId}'." + $"Message #{messageId} does not exist in the export of channel #{channelId}." ); } @@ -129,7 +129,7 @@ public static class ExportWrapper if (message.ValueKind == JsonValueKind.Undefined) { throw new InvalidOperationException( - $"Message '{messageId}' does not exist in the export of channel '{channelId}'." + $"Message #{messageId} does not exist in the export of channel #{channelId}." ); } diff --git a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs index f740e26..660b67b 100644 --- a/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs +++ b/DiscordChatExporter.Cli/Commands/Base/ExportCommandBase.cs @@ -168,7 +168,9 @@ public abstract class ExportCommandBase : DiscordCommandBase { throw new CommandException( "Attempted to export multiple channels, but the output path is neither a directory nor a template. " - + "If the provided output path is meant to be treated as a directory, make sure it ends with a slash." + + "If the provided output path is meant to be treated as a directory, make sure it ends with a slash. " + + "Provided output path: " + + OutputPath ); } diff --git a/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs b/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs index 637aaaf..e2554ce 100644 --- a/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs +++ b/DiscordChatExporter.Core/Exceptions/DiscordChatExporterException.cs @@ -6,8 +6,8 @@ public class DiscordChatExporterException : Exception { public bool IsFatal { get; } - public DiscordChatExporterException(string message, bool isFatal = false) - : base(message) + public DiscordChatExporterException(string message, bool isFatal = false, Exception? innerException = null) + : base(message, innerException) { IsFatal = isFatal; } diff --git a/DiscordChatExporter.Core/Exporting/ChannelExporter.cs b/DiscordChatExporter.Core/Exporting/ChannelExporter.cs index 276ff7e..f399299 100644 --- a/DiscordChatExporter.Core/Exporting/ChannelExporter.cs +++ b/DiscordChatExporter.Core/Exporting/ChannelExporter.cs @@ -22,7 +22,7 @@ public class ChannelExporter(DiscordClient discord) if (request.Channel.Kind == ChannelKind.GuildForum) { throw new DiscordChatExporterException( - "Channel is a forum and cannot be exported directly. " + $"Channel '{request.Channel.Name}' (#{request.Channel.Id}) is a forum and cannot be exported directly. " + "You need to pull its threads and export them individually." ); } @@ -30,14 +30,16 @@ public class ChannelExporter(DiscordClient discord) // Check if the channel is empty if (request.Channel.IsEmpty) { - throw new DiscordChatExporterException("Channel does not contain any messages."); + throw new DiscordChatExporterException( + $"Channel '{request.Channel.Name}' (#{request.Channel.Id}) does not contain any messages." + ); } // Check if the 'after' boundary is valid if (request.After is not null && !request.Channel.MayHaveMessagesAfter(request.After.Value)) { throw new DiscordChatExporterException( - "Channel does not contain any messages within the specified period." + $"Channel '{request.Channel.Name}' (#{request.Channel.Id}) does not contain any messages within the specified period." ); } @@ -48,7 +50,7 @@ public class ChannelExporter(DiscordClient discord) ) { throw new DiscordChatExporterException( - "Channel does not contain any messages within the specified period." + $"Channel '{request.Channel.Name}' (#{request.Channel.Id}) does not contain any messages within the specified period." ); } @@ -68,20 +70,32 @@ public class ChannelExporter(DiscordClient discord) ) ) { - // Resolve members for referenced users - foreach (var user in message.GetReferencedUsers()) - await context.PopulateMemberAsync(user, cancellationToken); + try + { + // Resolve members for referenced users + foreach (var user in message.GetReferencedUsers()) + await context.PopulateMemberAsync(user, cancellationToken); - // Export the message - if (request.MessageFilter.IsMatch(message)) - await messageExporter.ExportMessageAsync(message, cancellationToken); + // Export the message + if (request.MessageFilter.IsMatch(message)) + await messageExporter.ExportMessageAsync(message, cancellationToken); + } + catch (Exception ex) + { + // Provide more context to the exception, to simplify debugging based on error messages + throw new DiscordChatExporterException( + $"Failed to export message #{message.Id} in channel '{request.Channel.Name}' (#{request.Channel.Id}).", + ex is not DiscordChatExporterException dex || dex.IsFatal, + ex + ); + } } // Throw if no messages were exported if (messageExporter.MessagesExported <= 0) { throw new DiscordChatExporterException( - "Channel does not contain any matching messages within the specified period." + $"Channel '{request.Channel.Name}' (#{request.Channel.Id}) does not contain any matching messages within the specified period." ); } } diff --git a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs index 4dab3e1..bd893ef 100644 --- a/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs +++ b/DiscordChatExporter.Gui/ViewModels/Components/DashboardViewModel.cs @@ -277,9 +277,7 @@ public class DashboardViewModel : PropertyChangedBase } catch (DiscordChatExporterException ex) when (!ex.IsFatal) { - _eventAggregator.Publish( - new NotificationMessage(ex.Message.TrimEnd('.') + $" ({channel.Name})") - ); + _eventAggregator.Publish(new NotificationMessage(ex.Message.TrimEnd('.'))); } finally {