Skip to content

Commit

Permalink
Merge pull request #30 from AoshiW/catch-exception
Browse files Browse the repository at this point in the history
add ty/catch block in `RaiseEvent`
  • Loading branch information
Mahsaap authored Jun 17, 2024
2 parents bcc5801 + 400b11c commit 10a6910
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 13 additions & 3 deletions TwitchLib.EventSub.Websockets/EventSubWebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ private async Task HandleDisconnect(string message)

if (data != null)
_logger?.LogForceDisconnected(data.Payload.Session.Id, data.Payload.Session.DisconnectedAt, data.Payload.Session.DisconnectReason);

await WebsocketDisconnected.InvokeAsync(this, EventArgs.Empty);
}

Expand Down Expand Up @@ -665,16 +665,26 @@ private void HandleRevocation(string message)
/// </summary>
/// <param name="eventName">name of the event to raise</param>
/// <param name="args">args to pass with the event</param>
internal void RaiseEvent(string eventName, object args = null)
internal async void RaiseEvent(string eventName, object args = null)
{
var fInfo = GetType().GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic);

if (fInfo?.GetValue(this) is not MulticastDelegate multi)
return;

var parameters = new object[] { this, args ?? EventArgs.Empty };
foreach (var del in multi.GetInvocationList())
{
del.Method.Invoke(del.Target, args == null ? new object[] { this, EventArgs.Empty } : new[] { this, args });
try
{
var result = del.Method.Invoke(del.Target, parameters);
if (result is Task task)
await task;
}
catch (Exception ex)
{
_logger.LogRaiseEventExeption(eventName, ex);
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions TwitchLib.EventSub.Websockets/Extensions/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ namespace TwitchLib.EventSub.Websockets.Extensions
{
internal static partial class LogExtensions
{
[LoggerMessage(LogLevel.Error, "Exeption was throw when raising '{eventName}' event.")]
public static partial void LogRaiseEventExeption(this ILogger<EventSubWebsocketClient> logger, string eventName, Exception ex);

[LoggerMessage(LogLevel.Debug, "{message}")]
public static partial void LogMessage(this ILogger<EventSubWebsocketClient> logger, string message);

[LoggerMessage(LogLevel.Critical, "Websocket {sessionId} disconnected at {disconnectedAt}. Reason: {disconnectReason}")]
public static partial void LogForceDisconnected(this ILogger<EventSubWebsocketClient> logger, string sessionId, DateTime? disconnectedAt, string disconnectReason);

Expand All @@ -25,4 +28,4 @@ internal static partial class LogExtensions
[LoggerMessage(LogLevel.Critical, "{closeStatus} - {closeStatusDescription}")]
public static partial void LogWebsocketClosed(this ILogger<WebsocketClient> logger, WebSocketCloseStatus closeStatus, string closeStatusDescription);
}
}
}

0 comments on commit 10a6910

Please sign in to comment.