Skip to content

Commit

Permalink
SendAsync methods are wrapped into HttpRequestException
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMenshykh committed Jul 21, 2023
1 parent e78c188 commit 70db53b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public async Task UpdateAsync(string indexName, PineconeDocument document, strin
}
catch (HttpRequestException e)
{
this._logger.LogWarning("Vector update for Document {0} failed. Message: {1}", document.Id, e.Message);
this._logger.LogError(e, "Vector update for Document {0} failed. Message: {1}", document.Id, e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
}
Expand All @@ -338,7 +338,7 @@ public async Task UpdateAsync(string indexName, PineconeDocument document, strin
}
catch (HttpRequestException e)
{
this._logger.LogDebug("Index not found {0}", e.Message);
this._logger.LogError(e, "Index not found {0}", e.Message);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,21 @@ public async IAsyncEnumerable<string> ListCollectionsAsync([EnumeratorCancellati
this._logger.LogDebug("Listing collections");

using var request = ListCollectionsRequest.Create().Build();

(HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);

var collections = JsonSerializer.Deserialize<ListCollectionsResponse>(responseContent);

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
this._logger.LogError(e, "Unable to list collections: {0}, {1}", e.Message, responseContent);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}

foreach (var collection in collections?.Result?.Collections ?? Enumerable.Empty<ListCollectionsResponse.CollectionResult.CollectionDescription>())
{
yield return collection.Name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,30 @@ public async Task<bool> DoesCollectionExistAsync(string collectionName, Cancella

using HttpRequestMessage request = GetClassRequest.Create(className).Build();

(HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);

if (response.StatusCode == HttpStatusCode.NotFound)
{
this._logger.LogTrace("Collection: {0}, with class name: {1}, does not exist.", collectionName, className);
return false;
}

try
{
(HttpResponseMessage response, string responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

// Needs to return a non-404 AND collection name should match
bool exists = response.StatusCode != HttpStatusCode.NotFound;
if (!exists)
{
this._logger.LogTrace("Collection: {0}, with class name: {1}, does not exist.", collectionName, className);
}
else
GetClassResponse? existing = JsonSerializer.Deserialize<GetClassResponse>(responseContent, s_jsonSerializerOptions);
if (existing != null && existing.Description != ToWeaviateFriendlyClassDescription(collectionName))
{
GetClassResponse? existing = JsonSerializer.Deserialize<GetClassResponse>(responseContent, s_jsonSerializerOptions);
if (existing != null && existing.Description != ToWeaviateFriendlyClassDescription(collectionName))
{
// ReSharper disable once CommentTypo
// Check that we don't have an accidental conflict.
// For example a collectionName of '__this_collection' and 'this_collection' are
// both transformed to the class name of <classNamePrefix>thiscollection - even though the external
// system could consider them as unique collection names.
throw new SKException($"Unable to verify existing collection: {collectionName} with class name: {className}");
}
throw new SKException($"Unable to verify existing collection: {collectionName} with class name: {className}");
}

return exists;
return true;
}
catch (Exception e)
catch (HttpRequestException e)
{
throw new SKException("Unable to get class from Weaviate", e);
this._logger.LogError(e, "Unable to verify existing collection: {0}, with class name: {1}", collectionName, className);
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}
}

Expand Down Expand Up @@ -287,15 +282,22 @@ public async IAsyncEnumerable<string> UpsertBatchAsync(string collectionName, IE
}.Build();

string responseContent;

(HttpResponseMessage response, responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);

try
{
(HttpResponseMessage response, responseContent) = await this.ExecuteHttpRequestAsync(request, cancellationToken).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException) when (response.StatusCode == HttpStatusCode.NotFound)
{
this._logger.LogDebug("No vector with key: {0} is found", key);
return null;
}
catch (HttpRequestException e)
{
this._logger.LogError("Request for vector failed {0}", e.Message);
return null;
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}

WeaviateObject? weaviateObject = JsonSerializer.Deserialize<WeaviateObject>(responseContent, s_jsonSerializerOptions);
Expand Down
16 changes: 15 additions & 1 deletion dotnet/src/Skills/Skills.Core/HttpSkill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.SkillDefinition;

namespace Microsoft.SemanticKernel.Skills.Core;
Expand Down Expand Up @@ -103,7 +104,20 @@ public Task<string> DeleteAsync(
private async Task<string> SendRequestAsync(string uri, HttpMethod method, HttpContent? requestContent, CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage(method, uri) { Content = requestContent };

using var response = await this._client.SendAsync(request, cancellationToken).ConfigureAwait(false);
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);

var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

try
{
response.EnsureSuccessStatusCode();
}
catch (HttpRequestException e)
{
throw new HttpOperationException(response.StatusCode, responseContent, e.Message, e);
}

return responseContent;
}
}

0 comments on commit 70db53b

Please sign in to comment.