From c0ccb23e08fb5035ec9ceba18913f0ecf824af6d Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 12 Feb 2025 12:43:54 -0800 Subject: [PATCH] Log the response text if available if exception is thrown when processing query --- .../Microsoft.Azure.Agent/AzureAgent.cs | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/AzureAgent.cs b/shell/agents/Microsoft.Azure.Agent/AzureAgent.cs index f1bb3eb0..3a098ca7 100644 --- a/shell/agents/Microsoft.Azure.Agent/AzureAgent.cs +++ b/shell/agents/Microsoft.Azure.Agent/AzureAgent.cs @@ -161,6 +161,8 @@ public async Task RefreshChatAsync(IShell shell, bool force) { IHost host = shell.Host; CancellationToken cancellationToken = shell.CancellationToken; + + _copilotResponse = null; ResetArgumentPlaceholder(); try @@ -214,6 +216,9 @@ public async Task ChatAsync(string input, IShell shell) IHost host = shell.Host; CancellationToken token = shell.CancellationToken; + _copilotResponse = null; + ResetArgumentPlaceholder(); + if (_turnsLeft is 0) { host.WriteLine("\nSorry, you've reached the maximum length of a conversation. Please run '/refresh' to start a new conversation.\n"); @@ -243,8 +248,6 @@ public async Task ChatAsync(string input, IShell shell) if (_copilotResponse.ChunkReader is null) { - ResetArgumentPlaceholder(); - if (_copilotResponse.IsError) { string errorMessage = _copilotResponse.Text; @@ -325,11 +328,22 @@ public async Task ChatAsync(string input, IShell shell) Telemetry.Trace(AzTrace.Chat(_copilotResponse)); } - catch (Exception ex) when (ex is TokenRequestException or ConnectionDroppedException) + catch (Exception ex) { - host.WriteErrorLine(ex.Message); - host.WriteErrorLine("Please run '/refresh' to start a new chat session and try again."); - return false; + if (ex is TokenRequestException or ConnectionDroppedException) + { + host.WriteErrorLine(ex.Message); + host.WriteErrorLine("Please run '/refresh' to start a new chat session and try again."); + return false; + } + + Log.Error(ex, "Exception thrown when processing the query '{0}'", input); + if (_copilotResponse?.Text is not null) + { + Log.Error("Response text:\n{0}", _copilotResponse.Text); + } + + throw; } return true;