fix: yield error when initial stream response is not successful

This commit is contained in:
Stevan Freeborn
2024-07-19 21:15:59 -05:00
parent 0a692d1b9a
commit 3b47ed3fb4
2 changed files with 46 additions and 0 deletions
@@ -95,6 +95,14 @@ public class AnthropicApiClient : IAnthropicApiClient
public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request)
{
var response = await SendRequestAsync(request);
if (response.IsSuccessStatusCode is false)
{
var error = Deserialize<AnthropicError>(await response.Content.ReadAsStringAsync()) ?? new AnthropicError();
yield return new AnthropicEvent(EventType.Error, new ErrorEventData(error.Error));
yield break;
}
var anthropicHeaders = new AnthropicHeaders(response.Headers);
using var responseContent = await response.Content.ReadAsStreamAsync();