From e49cf997eab9c8089b307b30f1281e545f6f805d Mon Sep 17 00:00:00 2001 From: Frederic Portaria-Janicki Date: Tue, 2 Feb 2021 07:42:04 -0500 Subject: [PATCH] [CLI] Fix missing channel category bug when API returns empty string (#492) --- .../Discord/DiscordClient.cs | 15 +++++++++++++-- .../Discord/Models/ChannelCategory.cs | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/DiscordChatExporter.Domain/Discord/DiscordClient.cs b/DiscordChatExporter.Domain/Discord/DiscordClient.cs index 1cd1e084..ba19e2a2 100644 --- a/DiscordChatExporter.Domain/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Domain/Discord/DiscordClient.cs @@ -171,9 +171,20 @@ namespace DiscordChatExporter.Domain.Discord public async ValueTask GetChannelCategoryAsync(Snowflake channelId) { - var response = await GetJsonResponseAsync($"channels/{channelId}"); + try + { + var response = await GetJsonResponseAsync($"channels/{channelId}"); + return ChannelCategory.Parse(response); + } + /*** + * In some cases, the Discord API returns an empty body when requesting some channel category info. + * Instead, we use an empty channel category as a fallback. + */ + catch (DiscordChatExporterException) + { + return ChannelCategory.Empty; + } - return ChannelCategory.Parse(response); } public async ValueTask GetChannelAsync(Snowflake channelId) diff --git a/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs b/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs index e8d5471f..8fcdce26 100644 --- a/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs +++ b/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs @@ -44,5 +44,7 @@ namespace DiscordChatExporter.Domain.Discord.Models position.Value ); } + + public static ChannelCategory Empty { get; } = new(Snowflake.Zero, "Missing", 0); } -} \ No newline at end of file +}