Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle JsonException when deserializing answerJson #325

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion app/backend/Services/ReadRetrieveReadChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonElement>(answerJson);

JsonElement answerObject;
try
{
answerObject = JsonSerializer.Deserialize<JsonElement>(answerJson);
}
catch (JsonException)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
catch (JsonException)
catch (JsonException je)

{
throw new InvalidOperationException($"Unable to cast '{answerJson}' as JsonElement.");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw new InvalidOperationException($"Unable to cast '{answerJson}' as JsonElement.");
throw new InvalidOperationException(
message: $"Unable to cast '{answerJson}' as JsonElement.",
innerException: je);

}

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");

Expand Down