fix(bot): clean up "not found in cache" errors in sentry

This commit is contained in:
alyssa 2025-02-24 10:53:18 +00:00
parent 63777bf810
commit d74a6678c8
3 changed files with 22 additions and 2 deletions

View file

@ -9,14 +9,14 @@ public static class CacheExtensions
public static async Task<Guild> GetGuild(this IDiscordCache cache, ulong guildId) public static async Task<Guild> GetGuild(this IDiscordCache cache, ulong guildId)
{ {
if (!(await cache.TryGetGuild(guildId) is Guild guild)) if (!(await cache.TryGetGuild(guildId) is Guild guild))
throw new KeyNotFoundException($"Guild {guildId} not found in cache"); throw new NotFoundInCacheException(guildId, "guild");
return guild; return guild;
} }
public static async Task<Channel> GetChannel(this IDiscordCache cache, ulong guildId, ulong channelId) public static async Task<Channel> GetChannel(this IDiscordCache cache, ulong guildId, ulong channelId)
{ {
if (!(await cache.TryGetChannel(guildId, channelId) is Channel channel)) 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; return channel;
} }
@ -55,3 +55,15 @@ public static class CacheExtensions
return parent; 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;
}
}

View file

@ -213,6 +213,13 @@ public class Bot
{ {
_metrics.Measure.Meter.Mark(BotMetrics.BotErrors, exc.GetType().FullName); _metrics.Measure.Meter.Mark(BotMetrics.BotErrors, exc.GetType().FullName);
if (exc is Myriad.Extensions.NotFoundInCacheException ce)
{
var scope = serviceScope.Resolve<Scope>();
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 // Make this beforehand so we can access the event ID for logging
var sentryEvent = new SentryEvent(exc); var sentryEvent = new SentryEvent(exc);

View file

@ -949,6 +949,7 @@
"Microsoft.AspNetCore.Mvc.Versioning": "[5.1.0, )", "Microsoft.AspNetCore.Mvc.Versioning": "[5.1.0, )",
"Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer": "[5.1.0, )", "Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer": "[5.1.0, )",
"PluralKit.Core": "[1.0.0, )", "PluralKit.Core": "[1.0.0, )",
"Sentry": "[4.13.0, )",
"Serilog.AspNetCore": "[9.0.0, )" "Serilog.AspNetCore": "[9.0.0, )"
} }
}, },