Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/GenerativeAI.Live/Events/ErrorMessageEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace GenerativeAI.Live.Events;

/// <summary>
/// Provides error message event data.
/// </summary>
public class ErrorMessageEventArgs : EventArgs
{
/// <summary>
/// Gets the payload of the received message.
/// </summary>
public string ErrorMessage { get; }

/// <devdoc>
/// Initializes a new instance of the class.
/// </devdoc>
public ErrorMessageEventArgs(string errorMessage)
{
ErrorMessage = errorMessage;
}
}
6 changes: 6 additions & 0 deletions src/GenerativeAI.Live/Models/MultiModalLiveClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using GenerativeAI.Core;
using GenerativeAI.Live.Events;
using GenerativeAI.Live.Helper;
using GenerativeAI.Live.Logging;
using GenerativeAI.Types;
Expand Down Expand Up @@ -560,6 +561,11 @@ public async Task ConnectAsync(bool autoSendSetup = true,CancellationToken cance
//log info.CloseStatusDescription
_logger?.LogConnectionClosedWithInvalidPyload(info.CloseStatusDescription!);
}
else if (info.CloseStatus == WebSocketCloseStatus.InternalServerError && !string.IsNullOrEmpty(info.CloseStatusDescription))
{
_logger?.LogConnectionClosedWithError(info.Type, info.Exception!);
Disconnected?.Invoke(this, new ErrorMessageEventArgs(info.CloseStatusDescription));
}
Comment on lines +564 to +568
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid logging a null Exception; log the server’s close description instead.

In the InternalServerError path, info.Exception is often null for server-initiated closures. Passing info.Exception! to LogConnectionClosedWithError risks null-related issues and loses the actual reason provided by the server. Log the CloseStatus and CloseStatusDescription explicitly.

Apply this diff to make logging accurate and null-safe:

-            else if (info.CloseStatus == WebSocketCloseStatus.InternalServerError && !string.IsNullOrEmpty(info.CloseStatusDescription))
-            {
-                _logger?.LogConnectionClosedWithError(info.Type, info.Exception!);
-                Disconnected?.Invoke(this, new ErrorMessageEventArgs(info.CloseStatusDescription));
-            }
+            else if (info.CloseStatus == WebSocketCloseStatus.InternalServerError && !string.IsNullOrWhiteSpace(info.CloseStatusDescription))
+            {
+                _logger?.LogError("Connection closed by server with status {Status}. Reason: {Reason}", info.CloseStatus, info.CloseStatusDescription);
+                Disconnected?.Invoke(this, new ErrorMessageEventArgs(info.CloseStatusDescription));
+            }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
else if (info.CloseStatus == WebSocketCloseStatus.InternalServerError && !string.IsNullOrEmpty(info.CloseStatusDescription))
{
_logger?.LogConnectionClosedWithError(info.Type, info.Exception!);
Disconnected?.Invoke(this, new ErrorMessageEventArgs(info.CloseStatusDescription));
}
else if (info.CloseStatus == WebSocketCloseStatus.InternalServerError && !string.IsNullOrWhiteSpace(info.CloseStatusDescription))
{
_logger?.LogError(
"Connection closed by server with status {Status}. Reason: {Reason}",
info.CloseStatus,
info.CloseStatusDescription);
Disconnected?.Invoke(this, new ErrorMessageEventArgs(info.CloseStatusDescription));
}
🤖 Prompt for AI Agents
In src/GenerativeAI.Live/Models/MultiModalLiveClient.cs around lines 564 to 568,
the InternalServerError branch currently calls LogConnectionClosedWithError with
info.Exception! which can be null and loses the server-provided reason; change
the logging to be null-safe and include the CloseStatus and
CloseStatusDescription (e.g., call a logger method that accepts the close status
and description or build a message string combining info.CloseStatus and
info.CloseStatusDescription), and keep invoking Disconnected with the
CloseStatusDescription so the server reason is propagated without dereferencing
info.Exception.

else
{
_logger?.LogConnectionClosed();
Expand Down