Split error and warning printing into separate code paths

This commit is contained in:
Leonardo Mosquera 2025-04-01 08:43:38 -03:00
parent 3c0bf8a125
commit fb86fee58c

View file

@ -256,33 +256,48 @@ public abstract class ExportCommandBase : DiscordCommandBase
); );
} }
// Print errors and warnings // Print warnings
if (errorsByChannel.Any() || warningsByChannel.Any()) if (warningsByChannel.Any())
{ {
var tuples = new List<(ConcurrentDictionary<Channel, string>, ConsoleColor, string)> await console.Output.WriteLineAsync();
using (console.WithForegroundColor(ConsoleColor.Yellow))
{ {
(errorsByChannel, ConsoleColor.Red, "Failed to export the following channels:"), await console.Error.WriteLineAsync(
(warningsByChannel, ConsoleColor.Yellow, "No messages exported for the following channels:") $"Warnings reported for the following channel(s):"
}; );
foreach (var (messages, color, title) in tuples) {
if (! messages.Any())
continue;
await console.Output.WriteLineAsync();
using (console.WithForegroundColor(color))
await console.Error.WriteLineAsync(title);
foreach (var (channel, message) in messages)
{
await console.Error.WriteAsync($"{channel.GetHierarchicalName()}: ");
using (console.WithForegroundColor(color))
await console.Error.WriteLineAsync(message);
}
await console.Error.WriteLineAsync();
} }
foreach (var (channel, message) in warningsByChannel)
{
await console.Error.WriteAsync($"{channel.GetHierarchicalName()}: ");
using (console.WithForegroundColor(ConsoleColor.Yellow))
await console.Error.WriteLineAsync(message);
}
await console.Error.WriteLineAsync();
}
// Print errors
if (errorsByChannel.Any())
{
await console.Output.WriteLineAsync();
using (console.WithForegroundColor(ConsoleColor.Red))
{
await console.Error.WriteLineAsync(
$"Failed to export the following channel(s):"
);
}
foreach (var (channel, message) in errorsByChannel)
{
await console.Error.WriteAsync($"{channel.GetHierarchicalName()}: ");
using (console.WithForegroundColor(ConsoleColor.Red))
await console.Error.WriteLineAsync(message);
}
await console.Error.WriteLineAsync();
} }
// Fail the command only if ALL channels failed to export. // Fail the command only if ALL channels failed to export.