From 2152953b4b59ab1cbc0e60f1c9197ea9b4c61890 Mon Sep 17 00:00:00 2001 From: alyssa Date: Mon, 21 Oct 2024 11:58:31 +0900 Subject: [PATCH] fix(bot): bump avatar service http timeout, add better user-facing error --- .../Services/AvatarHostingService.cs | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/PluralKit.Bot/Services/AvatarHostingService.cs b/PluralKit.Bot/Services/AvatarHostingService.cs index a080bae8..6e88c4af 100644 --- a/PluralKit.Bot/Services/AvatarHostingService.cs +++ b/PluralKit.Bot/Services/AvatarHostingService.cs @@ -9,22 +9,35 @@ public class AvatarHostingService private readonly BotConfig _config; private readonly HttpClient _client; - public AvatarHostingService(BotConfig config, HttpClient client) + public AvatarHostingService(BotConfig config) { _config = config; - _client = client; + _client = new HttpClient + { + Timeout = TimeSpan.FromSeconds(10), + }; } public async Task TryRehostImage(ParsedImage input, RehostedImageType type, ulong userId, PKSystem? system) { - var uploaded = await TryUploadAvatar(input.Url, type, userId, system); - if (uploaded != null) + try { - // todo: make new image type called Cdn? - return new ParsedImage { Url = uploaded, Source = AvatarSource.HostedCdn }; - } + var uploaded = await TryUploadAvatar(input.Url, type, userId, system); + if (uploaded != null) + { + // todo: make new image type called Cdn? + return new ParsedImage { Url = uploaded, Source = AvatarSource.HostedCdn }; + } - return input; + return input; + } + catch (TaskCanceledException e) + { + // don't show an internal error to users + if (e.Message.Contains("HttpClient.Timeout")) + throw new PKError("Temporary error setting image, please try again later"); + throw; + } } public async Task TryUploadAvatar(string? avatarUrl, RehostedImageType type, ulong userId, PKSystem? system)