Skip to content

Commit de00797

Browse files
authored
Make Exception<T> base class non-generic and clean up derived types (#701)
### Motivation and Context Fixes #573 ### Description - Make the base class non-generic and rename it to SKException - Remove unnecessary private ctors from derived types - Clean up how messages are generated
1 parent 31de013 commit de00797

13 files changed

Lines changed: 489 additions & 431 deletions

File tree

‎dotnet/src/Connectors/Connectors.AI.OpenAI/CustomClient/OpenAIClientBase.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private async Task<T> ExecutePostRequestAsync<T>(string url, string requestBody,
200200

201201
if (response == null)
202202
{
203-
throw new AIException(AIException.ErrorCodes.NoResponse, "Empty response");
203+
throw new AIException(AIException.ErrorCodes.NoResponse);
204204
}
205205

206206
this.Log.LogTrace("HTTP response: {0} {1}", (int)response.StatusCode, response.StatusCode.ToString("G"));

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/Diagnostics/QdrantMemoryException.cs‎

Lines changed: 73 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,81 @@
55

66
namespace Microsoft.SemanticKernel.Connectors.Memory.Qdrant.Diagnostics;
77

8+
#pragma warning disable CA1032 // Implement standard exception constructors
9+
810
/// <summary>
9-
/// Custom exceptions for the Qdrant connector.
11+
/// Exception thrown for errors related to the Qdrant connector.
1012
/// </summary>
11-
public class QdrantMemoryException : Exception<QdrantMemoryException.ErrorCodes>
13+
public class QdrantMemoryException : SKException
1214
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with a provided error code.
17+
/// </summary>
18+
/// <param name="errorCode">The error code.</param>
19+
public QdrantMemoryException(ErrorCodes errorCode)
20+
: this(errorCode, message: null, innerException: null)
21+
{
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with a provided error code and message.
26+
/// </summary>
27+
/// <param name="errorCode">The error code.</param>
28+
/// <param name="message">The exception message.</param>
29+
public QdrantMemoryException(ErrorCodes errorCode, string? message)
30+
: this(errorCode, message, innerException: null)
31+
{
32+
}
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with a provided error code and inner exception.
36+
/// </summary>
37+
/// <param name="errorCode">The error code.</param>
38+
/// <param name="innerException">The exception that is the cause of the current exception.</param>
39+
public QdrantMemoryException(ErrorCodes errorCode, Exception? innerException)
40+
: this(errorCode, message: null, innerException)
41+
{
42+
}
43+
44+
/// <summary>
45+
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with a provided error code, message, and inner exception.
46+
/// </summary>
47+
/// <param name="errorCode">The error code.</param>
48+
/// <param name="message">A string that describes the error.</param>
49+
/// <param name="innerException">The exception that is the cause of the current exception.</param>
50+
public QdrantMemoryException(ErrorCodes errorCode, string? message, Exception? innerException)
51+
: base(GetDefaultMessage(errorCode, message, innerException), innerException)
52+
{
53+
this.ErrorCode = errorCode;
54+
}
55+
56+
/// <summary>
57+
/// Gets the error code for this exception.
58+
/// </summary>
59+
public ErrorCodes ErrorCode { get; }
60+
61+
/// <summary>Translate the error code into a default message.</summary>
62+
private static string GetDefaultMessage(ErrorCodes errorCode, string? message, Exception? innerException)
63+
{
64+
if (message is not null)
65+
{
66+
return message;
67+
}
68+
69+
string description = errorCode switch
70+
{
71+
ErrorCodes.UnableToDeserializeRecordPayload => "Unable to deserialize record payload",
72+
ErrorCodes.FailedToUpsertVectors => "Failed to upsert vectors",
73+
ErrorCodes.FailedToGetVectorData => "Failed to get vector data",
74+
ErrorCodes.FailedToRemoveVectorData => "Failed to remove vector data",
75+
ErrorCodes.FailedToConvertMemoryRecordToQdrantVectorRecord => "Failed to convert memory record to Qdrant vector record",
76+
ErrorCodes.FailedToConvertQdrantVectorRecordToMemoryRecord => "Failed to convert Qdrant vector record to memory record",
77+
_ => $"Unknown error ({errorCode:G})",
78+
};
79+
80+
return innerException is not null ? $"{description}: {innerException.Message}" : description;
81+
}
82+
1383
/// <summary>
1484
/// Error codes for the Qdrant connector exceptions.
1585
/// </summary>
@@ -18,7 +88,7 @@ public enum ErrorCodes
1888
/// <summary>
1989
/// Unknown error.
2090
/// </summary>
21-
UnknownError,
91+
UnknownError = -1,
2292

2393
/// <summary>
2494
/// Failed to deserialize the record payload.
@@ -50,47 +120,4 @@ public enum ErrorCodes
50120
/// </summary>
51121
FailedToConvertQdrantVectorRecordToMemoryRecord
52122
}
53-
54-
/// <summary>
55-
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with error code unknown.
56-
/// </summary>
57-
/// <param name="message">The exception message.</param>
58-
public QdrantMemoryException(string message)
59-
: this(ErrorCodes.UnknownError, message)
60-
{
61-
}
62-
63-
/// <summary>
64-
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with a provided error code.
65-
/// </summary>
66-
/// <param name="error">The error code.</param>
67-
/// <param name="message">The exception message.</param>
68-
public QdrantMemoryException(ErrorCodes error, string message)
69-
: base(error, message)
70-
{
71-
}
72-
73-
/// <summary>
74-
/// Initializes a new instance of the <see cref="QdrantMemoryException"/> class with an inner exception.
75-
/// </summary>
76-
/// <param name="error">The error code.</param>
77-
/// <param name="message">The exception message.</param>
78-
/// <param name="innerException">The inner exception</param>
79-
public QdrantMemoryException(ErrorCodes error, string message, Exception innerException)
80-
: base(BuildMessage(error, message), innerException)
81-
{
82-
}
83-
84-
private QdrantMemoryException()
85-
{
86-
}
87-
88-
private static string BuildMessage(ErrorCodes error, string? message)
89-
{
90-
return message != null ? $"{error.ToString("G")}: {message}" : error.ToString("G");
91-
}
92-
93-
private QdrantMemoryException(string message, System.Exception innerException) : base(message, innerException)
94-
{
95-
}
96123
}

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantMemoryStore.cs‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public async Task<string> UpsertAsync(string collectionName, MemoryRecord record
8585

8686
if (vectorData == null)
8787
{
88-
throw new QdrantMemoryException(QdrantMemoryException.ErrorCodes.FailedToConvertMemoryRecordToQdrantVectorRecord,
89-
$"Failed to convert MemoryRecord to QdrantVectorRecord");
88+
throw new QdrantMemoryException(QdrantMemoryException.ErrorCodes.FailedToConvertMemoryRecordToQdrantVectorRecord);
9089
}
9190

9291
try
@@ -100,7 +99,6 @@ await this._qdrantClient.UpsertVectorsAsync(
10099
{
101100
throw new QdrantMemoryException(
102101
QdrantMemoryException.ErrorCodes.FailedToUpsertVectors,
103-
$"Failed to upsert due to HttpRequestException: {ex.Message}",
104102
ex);
105103
}
106104

@@ -125,7 +123,6 @@ await this._qdrantClient.UpsertVectorsAsync(
125123
{
126124
throw new QdrantMemoryException(
127125
QdrantMemoryException.ErrorCodes.FailedToUpsertVectors,
128-
$"Failed to upsert due to HttpRequestException: {ex.Message}",
129126
ex);
130127
}
131128

@@ -157,14 +154,12 @@ await this._qdrantClient.UpsertVectorsAsync(
157154
{
158155
throw new QdrantMemoryException(
159156
QdrantMemoryException.ErrorCodes.FailedToGetVectorData,
160-
$"Failed to get vector data from Qdrant: {ex.Message}",
161157
ex);
162158
}
163159
catch (MemoryException ex)
164160
{
165161
throw new QdrantMemoryException(
166162
QdrantMemoryException.ErrorCodes.FailedToConvertQdrantVectorRecordToMemoryRecord,
167-
$"Failed deserialize Qdrant response to Memory Record: {ex.Message}",
168163
ex);
169164
}
170165
}
@@ -216,14 +211,12 @@ public async IAsyncEnumerable<MemoryRecord> GetBatchAsync(string collectionName,
216211
{
217212
throw new QdrantMemoryException(
218213
QdrantMemoryException.ErrorCodes.FailedToGetVectorData,
219-
$"Failed to get vector data from Qdrant: {ex.Message}",
220214
ex);
221215
}
222216
catch (MemoryException ex)
223217
{
224218
throw new QdrantMemoryException(
225219
QdrantMemoryException.ErrorCodes.FailedToConvertQdrantVectorRecordToMemoryRecord,
226-
$"Failed deserialize Qdrant response to Memory Record: {ex.Message}",
227220
ex);
228221
}
229222
}
@@ -262,7 +255,6 @@ public async Task RemoveAsync(string collectionName, string key, CancellationTok
262255
{
263256
throw new QdrantMemoryException(
264257
QdrantMemoryException.ErrorCodes.FailedToRemoveVectorData,
265-
$"Failed to remove vector data from Qdrant {ex.Message}",
266258
ex);
267259
}
268260
}
@@ -291,7 +283,6 @@ public async Task RemoveWithPointIdAsync(string collectionName, string pointId,
291283
{
292284
throw new QdrantMemoryException(
293285
QdrantMemoryException.ErrorCodes.FailedToRemoveVectorData,
294-
$"Failed to remove vector data from Qdrant {ex.Message}",
295286
ex);
296287
}
297288
}
@@ -314,7 +305,6 @@ public async Task RemoveWithPointIdBatchAsync(string collectionName, IEnumerable
314305
{
315306
throw new QdrantMemoryException(
316307
QdrantMemoryException.ErrorCodes.FailedToRemoveVectorData,
317-
$"Error in batch removing data from Qdrant {ex.Message}",
318308
ex);
319309
}
320310
}
@@ -433,8 +423,7 @@ private async Task<QdrantVectorRecord> ConvertFromMemoryRecordAsync(string colle
433423

434424
if (vectorData == null)
435425
{
436-
throw new QdrantMemoryException(QdrantMemoryException.ErrorCodes.FailedToConvertMemoryRecordToQdrantVectorRecord,
437-
$"Failed to convert MemoryRecord to QdrantVectorRecord");
426+
throw new QdrantMemoryException(QdrantMemoryException.ErrorCodes.FailedToConvertMemoryRecordToQdrantVectorRecord);
438427
}
439428

440429
return vectorData;

‎dotnet/src/Connectors/Connectors.Memory.Qdrant/QdrantVectorRecord.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static QdrantVectorRecord FromJsonMetadata(string pointId, IEnumerable<fl
8080
}
8181
else
8282
{
83-
throw new QdrantMemoryException(QdrantMemoryException.ErrorCodes.UnableToDeserializeRecordPayload, "Failed to deserialize payload");
83+
throw new QdrantMemoryException(QdrantMemoryException.ErrorCodes.UnableToDeserializeRecordPayload);
8484
}
8585
}
8686
}

0 commit comments

Comments
 (0)