From b87ec6ef0979907f203003f4fb41f8a6ed497aee Mon Sep 17 00:00:00 2001 From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:41:33 +0300 Subject: [PATCH] option to dump gw payloads on errors (#2999) * option to dump gw payloads on errors * w a r n i n g --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 9 +++++++++ src/Discord.Net.WebSocket/DiscordSocketConfig.cs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index d4754d7d..161fff3a 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -83,6 +83,7 @@ namespace Discord.WebSocket internal bool AlwaysResolveStickers { get; private set; } internal bool LogGatewayIntentWarnings { get; private set; } internal bool SuppressUnknownDispatchWarnings { get; private set; } + internal bool IncludeRawPayloadOnGatewayErrors { get; private set; } internal int AuditLogCacheSize { get; private set; } internal new DiscordSocketApiClient ApiClient => base.ApiClient; @@ -158,6 +159,7 @@ namespace Discord.WebSocket AlwaysResolveStickers = config.AlwaysResolveStickers; LogGatewayIntentWarnings = config.LogGatewayIntentWarnings; SuppressUnknownDispatchWarnings = config.SuppressUnknownDispatchWarnings; + IncludeRawPayloadOnGatewayErrors = config.IncludeRawPayloadOnGatewayErrors; HandlerTimeout = config.HandlerTimeout; State = new ClientState(0, 0); Rest = new DiscordSocketRestClient(config, ApiClient); @@ -3342,6 +3344,13 @@ namespace Discord.WebSocket } catch (Exception ex) { + if (IncludeRawPayloadOnGatewayErrors) + { + ex.Data["opcode"] = opCode; + ex.Data["type"] = type; + ex.Data["payload_data"] = (payload as JToken).ToString(); + } + await _gatewayLogger.ErrorAsync($"Error handling {opCode}{(type != null ? $" ({type})" : "")}", ex).ConfigureAwait(false); } } diff --git a/src/Discord.Net.WebSocket/DiscordSocketConfig.cs b/src/Discord.Net.WebSocket/DiscordSocketConfig.cs index 21f21a28..01849b72 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketConfig.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketConfig.cs @@ -176,6 +176,15 @@ namespace Discord.WebSocket } } + /// + /// Gets or sets whether or not to include the raw payload on gateway errors. + /// + /// + /// Note that this may expose sensitive information to logs. It is recommended to only enable this in + /// cases where you are actively debugging an issue. + /// + public bool IncludeRawPayloadOnGatewayErrors { get; set; } = false; + private int maxWaitForGuildAvailable = 10000; ///