From d74a6678c85bba13076a2b21d7d68ab119d7d1bd Mon Sep 17 00:00:00 2001 From: alyssa Date: Mon, 24 Feb 2025 10:53:18 +0000 Subject: [PATCH] fix(bot): clean up "not found in cache" errors in sentry --- Myriad/Extensions/CacheExtensions.cs | 16 ++++++++++++++-- PluralKit.Bot/Bot.cs | 7 +++++++ PluralKit.Tests/packages.lock.json | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Myriad/Extensions/CacheExtensions.cs b/Myriad/Extensions/CacheExtensions.cs index 84045784..084450bf 100644 --- a/Myriad/Extensions/CacheExtensions.cs +++ b/Myriad/Extensions/CacheExtensions.cs @@ -9,14 +9,14 @@ public static class CacheExtensions public static async Task GetGuild(this IDiscordCache cache, ulong guildId) { if (!(await cache.TryGetGuild(guildId) is Guild guild)) - throw new KeyNotFoundException($"Guild {guildId} not found in cache"); + throw new NotFoundInCacheException(guildId, "guild"); return guild; } public static async Task GetChannel(this IDiscordCache cache, ulong guildId, ulong channelId) { if (!(await cache.TryGetChannel(guildId, channelId) is Channel channel)) - throw new KeyNotFoundException($"Channel {channelId} not found in cache"); + throw new NotFoundInCacheException(channelId, "channel"); return channel; } @@ -54,4 +54,16 @@ public static class CacheExtensions if (parent == null) throw new Exception($"failed to find parent channel for thread {channelOrThread} in cache"); return parent; } +} + +public class NotFoundInCacheException: Exception +{ + public ulong EntityId { get; init; } + public string EntityType { get; init; } + + public NotFoundInCacheException(ulong id, string type) : base("expected entity in discord cache but was not found") + { + EntityId = id; + EntityType = type; + } } \ No newline at end of file diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index 6e15799c..d74428ee 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -213,6 +213,13 @@ public class Bot { _metrics.Measure.Meter.Mark(BotMetrics.BotErrors, exc.GetType().FullName); + if (exc is Myriad.Extensions.NotFoundInCacheException ce) + { + var scope = serviceScope.Resolve(); + scope.SetTag("entity.id", ce.EntityId.ToString()); + scope.SetTag("entity.type", ce.EntityType); + } + // Make this beforehand so we can access the event ID for logging var sentryEvent = new SentryEvent(exc); diff --git a/PluralKit.Tests/packages.lock.json b/PluralKit.Tests/packages.lock.json index 419ec053..8946e898 100644 --- a/PluralKit.Tests/packages.lock.json +++ b/PluralKit.Tests/packages.lock.json @@ -949,6 +949,7 @@ "Microsoft.AspNetCore.Mvc.Versioning": "[5.1.0, )", "Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer": "[5.1.0, )", "PluralKit.Core": "[1.0.0, )", + "Sentry": "[4.13.0, )", "Serilog.AspNetCore": "[9.0.0, )" } },