Skip to content

Commit 2dcf2ba

Browse files
committed
fix error handling
Refactors the chat response handling to streamline the retry logic and error handling. Adds logging for chat response failures to improve debugging and monitoring.
1 parent 65e7b29 commit 2dcf2ba

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

Pacos/Services/ChatCommandHandlers/MentionHandler.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,28 +106,20 @@ private async Task<ChatResponseInfo> GetChatResponseWithRetryAsync(
106106
byte[]? fileBytes = null,
107107
string? fileMimeType = null)
108108
{
109-
var result = await Policy
109+
return await Policy
110110
.Handle<ApiException>(x => x.ErrorCode is 502 or 503 or 504
111111
|| x.ErrorMessage?.Contains("try again", StringComparison.OrdinalIgnoreCase) == true)
112112
.Or<HttpRequestException>()
113113
.OrResult<ChatResponseInfo>(x => string.IsNullOrWhiteSpace(x.Text) && x.DataContents.Count == 0)
114114
.WaitAndRetryAsync(retryCount: 2, retryNumber => TimeSpan.FromMilliseconds(retryNumber * 200))
115-
.ExecuteAndCaptureAsync(async () => await _chatService.GetResponseAsync(
115+
.ExecuteAsync(async () => await _chatService.GetResponseAsync(
116116
chatId,
117117
messageId,
118118
authorName,
119119
messageText,
120120
fileBytes,
121121
fileMimeType
122122
));
123-
124-
return result switch
125-
{
126-
{ Outcome: OutcomeType.Failure, FinalException: not null } => throw result.FinalException,
127-
{ Outcome: OutcomeType.Failure, FinalException: null } when string.IsNullOrWhiteSpace(result.Result.Text) => throw new InvalidOperationException("Empty AI response."),
128-
{ Outcome: OutcomeType.Failure, FinalException: null } => throw new InvalidOperationException("Unexpected failure without an exception."),
129-
_ => result.Result,
130-
};
131123
}
132124

133125
public async Task HandleMentionAsync(
@@ -234,23 +226,14 @@ _ when _wordFilter.ContainsBannedWords(fullMessageToLlm) => "ты пидор, к
234226
}
235227
catch (Exception e)
236228
{
229+
_logger.LogError(e, "Failed to get chat response for {Author}", author);
237230
replyText = $"{e.GetType().Name}: {e.Message}";
238231
}
239232

240233
var markdownReplyText = _markdownConversionService.ConvertToTelegramMarkdown(replyText);
241234

242235
_logger.LogInformation("Replying to {Author} with: {ReplyText}", author, replyText);
243236

244-
async Task SendReply(string text, ParseMode parseMode)
245-
{
246-
await botClient.SendMessage(
247-
new ChatId(updateMessage.Chat.Id),
248-
text,
249-
parseMode,
250-
new ReplyParameters { MessageId = updateMessage.MessageId, },
251-
cancellationToken: cancellationToken);
252-
}
253-
254237
try
255238
{
256239
await SendReply(markdownReplyText, ParseMode.MarkdownV2);
@@ -260,5 +243,17 @@ await botClient.SendMessage(
260243
_logger.LogError(e, "Failed to send message with MarkdownV2. Falling back to plain text");
261244
await SendReply(replyText, ParseMode.None);
262245
}
246+
247+
return;
248+
249+
async Task SendReply(string text, ParseMode parseMode)
250+
{
251+
await botClient.SendMessage(
252+
new ChatId(updateMessage.Chat.Id),
253+
text,
254+
parseMode,
255+
new ReplyParameters { MessageId = updateMessage.MessageId, },
256+
cancellationToken: cancellationToken);
257+
}
263258
}
264259
}

0 commit comments

Comments
 (0)