From ebf7bb49791376b84cc140e476095e34e8703e4f Mon Sep 17 00:00:00 2001 From: sabihoshi Date: Sat, 23 Jul 2022 01:09:57 +0800 Subject: [PATCH] fix: Handle 404 NotFound in GetDominantColorAsync --- HuTao.Services/Image/ImageService.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/HuTao.Services/Image/ImageService.cs b/HuTao.Services/Image/ImageService.cs index 5adf628..2533a43 100644 --- a/HuTao.Services/Image/ImageService.cs +++ b/HuTao.Services/Image/ImageService.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using Discord; @@ -102,8 +103,15 @@ public async ValueTask GetDominantColorAsync(Uri location) if (_cache.TryGetValue(key, out Color color)) return color; - var imageBytes = await _httpClientFactory.CreateClient().GetByteArrayAsync(location); - return _cache.Set(key, GetDominantColor(imageBytes), TimeSpan.FromDays(7)); + try + { + var imageBytes = await _httpClientFactory.CreateClient().GetByteArrayAsync(location); + return _cache.Set(key, GetDominantColor(imageBytes), TimeSpan.FromHours(1)); + } + catch (HttpRequestException e) when (e.StatusCode is HttpStatusCode.NotFound) + { + return Color.Default; + } } private static object GetKey(Uri uri) => new { Target = nameof(GetDominantColor), uri.AbsoluteUri };