From 71890edd12b5f2dda87011503a6c8acd55c9c8f3 Mon Sep 17 00:00:00 2001 From: Jaliya Udagedara Date: Sat, 8 Jun 2024 07:58:00 +1200 Subject: [PATCH] Handle JsonException when deserializing answerJson --- app/backend/Services/ReadRetrieveReadChatService.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/backend/Services/ReadRetrieveReadChatService.cs b/app/backend/Services/ReadRetrieveReadChatService.cs index 7c72dcd9..def635be 100644 --- a/app/backend/Services/ReadRetrieveReadChatService.cs +++ b/app/backend/Services/ReadRetrieveReadChatService.cs @@ -191,7 +191,17 @@ You answer needs to be a json object with the following format. promptExecutingSetting, cancellationToken: cancellationToken); var answerJson = answer.Content ?? throw new InvalidOperationException("Failed to get search query"); - var answerObject = JsonSerializer.Deserialize(answerJson); + + JsonElement answerObject; + try + { + answerObject = JsonSerializer.Deserialize(answerJson); + } + catch (JsonException) + { + throw new InvalidOperationException($"Unable to cast '{answerJson}' as JsonElement."); + } + var ans = answerObject.GetProperty("answer").GetString() ?? throw new InvalidOperationException("Failed to get answer"); var thoughts = answerObject.GetProperty("thoughts").GetString() ?? throw new InvalidOperationException("Failed to get thoughts");