From aadb9a5280df9b8037cd82e8c9086013ad2ba304 Mon Sep 17 00:00:00 2001 From: AoshiW Date: Mon, 25 Mar 2024 21:36:16 +0100 Subject: [PATCH] catch parsing error --- TwitchLib.Client/Extensions/LogExtensions.cs | 3 +++ TwitchLib.Client/TwitchClient.cs | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/TwitchLib.Client/Extensions/LogExtensions.cs b/TwitchLib.Client/Extensions/LogExtensions.cs index ceaea67..7aff158 100644 --- a/TwitchLib.Client/Extensions/LogExtensions.cs +++ b/TwitchLib.Client/Extensions/LogExtensions.cs @@ -37,6 +37,9 @@ internal static partial class LogExtensions [LoggerMessage(LogLevel.Warning, "Unaccounted for: {ircString} (please create a TwitchLib GitHub issue :P)")] public static partial void LogUnaccountedFor(this ILogger logger, string ircString); + [LoggerMessage(LogLevel.Error, "Unexpected error during message parsing, message: {message}")] + public static partial void LogParsingError(this ILogger logger, string message, Exception ex); + [LoggerMessage(LogLevel.Debug, "Writing: {message}")] public static partial void LogWriting(this ILogger logger, string message); diff --git a/TwitchLib.Client/TwitchClient.cs b/TwitchLib.Client/TwitchClient.cs index 3fbc389..88c1c4a 100644 --- a/TwitchLib.Client/TwitchClient.cs +++ b/TwitchLib.Client/TwitchClient.cs @@ -699,7 +699,18 @@ private async Task _client_OnMessage(object? sender, OnMessageEventArgs e) _logger?.LogReceived(line); await OnSendReceiveData.TryInvoke(this, new(SendReceiveDirection.Received, line)); - await HandleIrcMessageAsync(IrcParser.ParseMessage(line)); + IrcMessage ircMessage; + try + { + ircMessage = IrcParser.ParseMessage(line); + } + catch (Exception ex) + { + _logger?.LogParsingError(line, ex); + OnError?.Invoke(this, new(ex)); + continue; + } + await HandleIrcMessageAsync(ircMessage); } }