Skip to content

Commit

Permalink
Handle internal server error in the AuditOverwritesChangedHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha12 committed Jan 15, 2024
1 parent 88979ce commit 473dfd8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/GrillBot.App/Actions/Commands/CleanChannelMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,5 @@ private async Task DeleteMessageAsync(IMessage message)
}

private static bool IsExpectedApiError(HttpException ex)
{
return ex.HttpCode == HttpStatusCode.InternalServerError || ex.HttpCode == HttpStatusCode.ServiceUnavailable || ex.DiscordCode == DiscordErrorCode.UnknownChannel ||
ex.DiscordCode == DiscordErrorCode.UnknownMessage;
}
=> ex.IsExpectedOutageError() || ex.DiscordCode == DiscordErrorCode.UnknownChannel || ex.DiscordCode == DiscordErrorCode.UnknownMessage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
using GrillBot.Common.Managers.Events.Contracts;
using GrillBot.Core.Services.AuditLog;
using GrillBot.Core.Services.AuditLog.Enums;
using GrillBot.Core.Services.AuditLog.Models;
using GrillBot.Core.Services.AuditLog.Models.Request.CreateItems;
using GrillBot.Core.Managers.Performance;
using Discord.Net;
using GrillBot.Common.Extensions.Discord;

namespace GrillBot.App.Handlers.ChannelUpdated;

Expand Down Expand Up @@ -52,7 +53,14 @@ private async Task<List<IAuditLogEntry>> GetAuditLogsAsync(IGuildChannel channel
IReadOnlyCollection<IAuditLogEntry> auditLogs;
using (CounterManager.Create("Discord.API.AuditLog"))
{
auditLogs = await channel.Guild.GetAuditLogsAsync(actionType: actionType);
try
{
auditLogs = await channel.Guild.GetAuditLogsAsync(actionType: actionType);
}
catch (HttpException ex) when (ex.IsExpectedOutageError())
{
auditLogs = new List<IAuditLogEntry>().AsReadOnly();
}
}

return auditLogs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ private async Task<List<IMessage>> DownloadMessagesFromChannelAsync(IMessageChan
/// Expected is InternalServerError, ServiceUnavailable, UnknownChannel and UnknownMessage.
/// </summary>
private static bool IsApiExpectedError(HttpException ex)
{
return ex.HttpCode == HttpStatusCode.InternalServerError || ex.HttpCode == HttpStatusCode.ServiceUnavailable || ex.DiscordCode == DiscordErrorCode.UnknownChannel ||
ex.DiscordCode == DiscordErrorCode.UnknownMessage;
}
=> ex.IsExpectedOutageError() || ex.DiscordCode == DiscordErrorCode.UnknownChannel || ex.DiscordCode == DiscordErrorCode.UnknownMessage;

private async Task ProcessDownloadedMessages(List<IMessage> messages, bool forceDelete)
{
Expand Down
10 changes: 10 additions & 0 deletions src/GrillBot.Common/Extensions/Discord/HttpExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Discord.Net;
using System.Net;

namespace GrillBot.Common.Extensions.Discord;

public static class HttpExceptionExtensions
{
public static bool IsExpectedOutageError(this HttpException ex)
=> ex.HttpCode == HttpStatusCode.InternalServerError || ex.HttpCode == HttpStatusCode.ServiceUnavailable;
}

0 comments on commit 473dfd8

Please sign in to comment.