From 138d3441e45c89add7914f0ee2a53e26ddcf87e7 Mon Sep 17 00:00:00 2001 From: Toastie <toastie@toastiet0ast.com> Date: Mon, 3 Feb 2025 15:37:48 +1300 Subject: [PATCH] OpenAI apis will now correctly print an error to the console if the request fails --- .../Games/ChatterBot/_common/OpenAiApiSession.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/EllieBot/Modules/Games/ChatterBot/_common/OpenAiApiSession.cs b/src/EllieBot/Modules/Games/ChatterBot/_common/OpenAiApiSession.cs index 42afd22..64aacbc 100644 --- a/src/EllieBot/Modules/Games/ChatterBot/_common/OpenAiApiSession.cs +++ b/src/EllieBot/Modules/Games/ChatterBot/_common/OpenAiApiSession.cs @@ -111,8 +111,20 @@ public partial class OpenAiApiSession : IChatterBotSession }); var dataString = await data.Content.ReadAsStringAsync(); + try { + data.EnsureSuccessStatusCode(); + } + catch (Exception ex) + { + Log.Error(ex, "Failed to get response from OpenAI: {Message}", ex.Message); + return new Error<string>("Failed to get response from OpenAI"); + } + + try + { + var response = JsonConvert.DeserializeObject<OpenAiCompletionResponse>(dataString); // Log.Information("Received response: {Response} ", dataString);