Provide more context in exception messages

pull/1160/head
Tyrrrz 11 months ago
parent a46bf9bf11
commit e89701e3f9

@ -105,7 +105,7 @@ public static class ExportWrapper
if (message is null) if (message is null)
{ {
throw new InvalidOperationException( 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) if (message.ValueKind == JsonValueKind.Undefined)
{ {
throw new InvalidOperationException( 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}."
); );
} }

@ -168,7 +168,9 @@ public abstract class ExportCommandBase : DiscordCommandBase
{ {
throw new CommandException( throw new CommandException(
"Attempted to export multiple channels, but the output path is neither a directory nor a template. " "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
); );
} }

@ -6,8 +6,8 @@ public class DiscordChatExporterException : Exception
{ {
public bool IsFatal { get; } public bool IsFatal { get; }
public DiscordChatExporterException(string message, bool isFatal = false) public DiscordChatExporterException(string message, bool isFatal = false, Exception? innerException = null)
: base(message) : base(message, innerException)
{ {
IsFatal = isFatal; IsFatal = isFatal;
} }

@ -22,7 +22,7 @@ public class ChannelExporter(DiscordClient discord)
if (request.Channel.Kind == ChannelKind.GuildForum) if (request.Channel.Kind == ChannelKind.GuildForum)
{ {
throw new DiscordChatExporterException( 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." + "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 // Check if the channel is empty
if (request.Channel.IsEmpty) 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 // Check if the 'after' boundary is valid
if (request.After is not null && !request.Channel.MayHaveMessagesAfter(request.After.Value)) if (request.After is not null && !request.Channel.MayHaveMessagesAfter(request.After.Value))
{ {
throw new DiscordChatExporterException( 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( 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 try
foreach (var user in message.GetReferencedUsers()) {
await context.PopulateMemberAsync(user, cancellationToken); // Resolve members for referenced users
foreach (var user in message.GetReferencedUsers())
await context.PopulateMemberAsync(user, cancellationToken);
// Export the message // Export the message
if (request.MessageFilter.IsMatch(message)) if (request.MessageFilter.IsMatch(message))
await messageExporter.ExportMessageAsync(message, cancellationToken); 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 // Throw if no messages were exported
if (messageExporter.MessagesExported <= 0) if (messageExporter.MessagesExported <= 0)
{ {
throw new DiscordChatExporterException( 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."
); );
} }
} }

@ -277,9 +277,7 @@ public class DashboardViewModel : PropertyChangedBase
} }
catch (DiscordChatExporterException ex) when (!ex.IsFatal) catch (DiscordChatExporterException ex) when (!ex.IsFatal)
{ {
_eventAggregator.Publish( _eventAggregator.Publish(new NotificationMessage(ex.Message.TrimEnd('.')));
new NotificationMessage(ex.Message.TrimEnd('.') + $" ({channel.Name})")
);
} }
finally finally
{ {

Loading…
Cancel
Save