Skip to content

Commit c879204

Browse files
committed
Refactoring
1 parent cd0d270 commit c879204

File tree

13 files changed

+76
-17
lines changed

13 files changed

+76
-17
lines changed

extensions/Anthropic/Client/RawAnthropicClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ internal async IAsyncEnumerable<StreamingResponseMessage> CallClaudeStreamingAsy
6464
if (!response.IsSuccessStatusCode)
6565
{
6666
var responseError = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
67-
var isTransient = (new List<int> { 500, 502, 503, 504 }).Contains((int)response.StatusCode);
68-
throw new KernelMemoryException($"Failed to send request: {response.StatusCode} - {responseError}", isTransient: isTransient);
67+
throw new KernelMemoryException($"Failed to send request: {response.StatusCode} - {responseError}",
68+
isTransient: response.StatusCode.IsTransientError());
6969
}
7070

7171
var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);

extensions/AzureAIDocIntel/AzureAIDocIntelEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public async Task<string> ExtractTextFromImageAsync(Stream imageContent, Cancell
6868

6969
return operationResponse.Value.Content;
7070
}
71-
catch (RequestFailedException e) when (e.Status is >= 400 and < 500)
71+
catch (RequestFailedException e) when (HttpErrors.IsFatalError(e.Status))
7272
{
7373
throw new AzureAIDocIntelException(e.Message, e, isTransient: false);
7474
}

extensions/AzureOpenAI/AzureOpenAI/AzureOpenAITextEmbeddingGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public Task<Embedding> GenerateEmbeddingAsync(string text, CancellationToken can
126126
{
127127
return this._client.GenerateEmbeddingAsync(text, cancellationToken);
128128
}
129-
catch (HttpOperationException e) when (e.StatusCode.HasValue && (int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
129+
catch (HttpOperationException e) when (e.StatusCode.IsFatalError())
130130
{
131131
throw new AzureOpenAIException(e.Message, e, isTransient: false);
132132
}
@@ -142,7 +142,7 @@ public async Task<Embedding[]> GenerateEmbeddingBatchAsync(IEnumerable<string> t
142142
IList<ReadOnlyMemory<float>> embeddings = await this._client.GenerateEmbeddingsAsync(list, cancellationToken: cancellationToken).ConfigureAwait(false);
143143
return embeddings.Select(e => new Embedding(e)).ToArray();
144144
}
145-
catch (HttpOperationException e) when (e.StatusCode.HasValue && (int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
145+
catch (HttpOperationException e) when (e.StatusCode.IsFatalError())
146146
{
147147
throw new AzureOpenAIException(e.Message, e, isTransient: false);
148148
}

extensions/AzureOpenAI/AzureOpenAI/AzureOpenAITextGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public async IAsyncEnumerable<string> GenerateTextAsync(
146146
{
147147
result = this._client.GetStreamingTextContentsAsync(prompt, skOptions, cancellationToken: cancellationToken);
148148
}
149-
catch (HttpOperationException e) when (e.StatusCode.HasValue && (int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
149+
catch (HttpOperationException e) when (e.StatusCode.IsFatalError())
150150
{
151151
throw new AzureOpenAIException(e.Message, e, isTransient: false);
152152
}

extensions/AzureQueues/AzureQueuesPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void OnDequeue(Func<string, Task<ResultType>> processMessageAction)
209209
await this.UnlockMessageAsync(message, backoffDelay, cancellationToken: default).ConfigureAwait(false);
210210
break;
211211

212-
case ResultType.UnrecoverableError:
212+
case ResultType.FatalError:
213213
this._log.LogError("Message '{0}' failed to process due to a non-recoverable error, moving to poison queue", message.MessageId);
214214
await this.MoveMessageToPoisonQueueAsync(message, cancellationToken: default).ConfigureAwait(false);
215215
break;

extensions/OpenAI/OpenAI/OpenAITextEmbeddingGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public Task<Embedding> GenerateEmbeddingAsync(string text, CancellationToken can
127127
{
128128
return this._client.GenerateEmbeddingAsync(text, cancellationToken);
129129
}
130-
catch (HttpOperationException e) when (e.StatusCode.HasValue && (int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
130+
catch (HttpOperationException e) when (e.StatusCode.IsFatalError())
131131
{
132132
throw new OpenAIException(e.Message, e, isTransient: false);
133133
}
@@ -143,7 +143,7 @@ public async Task<Embedding[]> GenerateEmbeddingBatchAsync(IEnumerable<string> t
143143
var embeddings = await this._client.GenerateEmbeddingsAsync(list, cancellationToken: cancellationToken).ConfigureAwait(false);
144144
return embeddings.Select(e => new Embedding(e)).ToArray();
145145
}
146-
catch (HttpOperationException e) when (e.StatusCode.HasValue && (int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
146+
catch (HttpOperationException e) when (e.StatusCode.IsFatalError())
147147
{
148148
throw new OpenAIException(e.Message, e, isTransient: false);
149149
}

extensions/OpenAI/OpenAI/OpenAITextGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public async IAsyncEnumerable<string> GenerateTextAsync(
146146
{
147147
result = this._client.GetStreamingTextContentsAsync(prompt, skOptions, cancellationToken: cancellationToken);
148148
}
149-
catch (HttpOperationException e) when (e.StatusCode.HasValue && (int)e.StatusCode >= 400 && (int)e.StatusCode < 500)
149+
catch (HttpOperationException e) when (e.StatusCode.IsFatalError())
150150
{
151151
throw new OpenAIException(e.Message, e, isTransient: false);
152152
}

extensions/RabbitMQ/RabbitMQ/RabbitMQPipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public void OnDequeue(Func<string, Task<ResultType>> processMessageAction)
220220
this._channel.BasicNack(args.DeliveryTag, multiple: false, requeue: true);
221221
break;
222222

223-
case ResultType.UnrecoverableError:
223+
case ResultType.FatalError:
224224
this._log.LogError("Message '{0}' failed to process due to a non-recoverable error, moving to poison queue", args.BasicProperties?.MessageId);
225225
this._channel.BasicNack(args.DeliveryTag, multiple: false, requeue: false);
226226
break;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System.Collections.Generic;
4+
using System.Net;
5+
6+
namespace Microsoft.KernelMemory.Diagnostics;
7+
8+
public static class HttpErrors
9+
{
10+
// Errors that might disappear by retrying
11+
private static readonly HashSet<int> s_transientErrors =
12+
[
13+
(int)HttpStatusCode.InternalServerError,
14+
(int)HttpStatusCode.BadGateway,
15+
(int)HttpStatusCode.ServiceUnavailable,
16+
(int)HttpStatusCode.GatewayTimeout,
17+
(int)HttpStatusCode.InsufficientStorage
18+
];
19+
20+
public static bool IsTransientError(this HttpStatusCode statusCode)
21+
{
22+
return s_transientErrors.Contains((int)statusCode);
23+
}
24+
25+
public static bool IsTransientError(this HttpStatusCode? statusCode)
26+
{
27+
return statusCode.HasValue && s_transientErrors.Contains((int)statusCode.Value);
28+
}
29+
30+
public static bool IsTransientError(int statusCode)
31+
{
32+
return s_transientErrors.Contains(statusCode);
33+
}
34+
35+
public static bool IsFatalError(this HttpStatusCode statusCode)
36+
{
37+
return IsError(statusCode) && !IsTransientError(statusCode);
38+
}
39+
40+
public static bool IsFatalError(this HttpStatusCode? statusCode)
41+
{
42+
return statusCode.HasValue && IsError(statusCode) && !IsTransientError(statusCode);
43+
}
44+
45+
public static bool IsFatalError(int statusCode)
46+
{
47+
return IsError(statusCode) && !IsTransientError(statusCode);
48+
}
49+
50+
private static bool IsError(this HttpStatusCode? statusCode)
51+
{
52+
return statusCode.HasValue && (int)statusCode.Value >= 400;
53+
}
54+
55+
private static bool IsError(int statusCode)
56+
{
57+
return statusCode >= 400;
58+
}
59+
}

service/Abstractions/Pipeline/ResultType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ public enum ResultType
66
{
77
Success = 0,
88
TransientError = 1,
9-
UnrecoverableError = 2,
9+
FatalError = 2,
1010
}

0 commit comments

Comments
 (0)