mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2024-09-19 04:08:45 -04:00
Provide more context in exception messages
This commit is contained in:
parent
a46bf9bf11
commit
e89701e3f9
5 changed files with 33 additions and 19 deletions
|
@ -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}."
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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."
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue