Skip to content

Commit c413991

Browse files
committed
feat: Add BadRequest and UnprocessableEntity exception handling in ExceptionHelper
1 parent 74043c8 commit c413991

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

src/Weaviate.Client.Tests/Unit/ExceptionHelperTests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,46 @@ public void MapGrpcException_VectorizationError_ReturnsExternalModuleProblemExce
273273
// Assert
274274
Assert.IsType<WeaviateExternalModuleProblemException>(result);
275275
}
276+
277+
[Fact]
278+
public void MapHttpException_BadRequest_ReturnsBadRequestException()
279+
{
280+
// Arrange
281+
var innerEx = new Weaviate.Client.Rest.WeaviateUnexpectedStatusCodeException(
282+
HttpStatusCode.BadRequest,
283+
new HashSet<HttpStatusCode> { HttpStatusCode.OK },
284+
"Bad request"
285+
);
286+
287+
// Act
288+
var result = ExceptionHelper.MapHttpException(
289+
HttpStatusCode.BadRequest,
290+
"Bad request",
291+
innerEx
292+
);
293+
294+
// Assert
295+
Assert.IsType<WeaviateBadRequestException>(result);
296+
}
297+
298+
[Fact]
299+
public void MapHttpException_UnprocessableEntity_ReturnsUnprocessableEntityException()
300+
{
301+
// Arrange
302+
var innerEx = new Weaviate.Client.Rest.WeaviateUnexpectedStatusCodeException(
303+
HttpStatusCode.UnprocessableEntity,
304+
new HashSet<HttpStatusCode> { HttpStatusCode.OK },
305+
"Unprocessable entity"
306+
);
307+
308+
// Act
309+
var result = ExceptionHelper.MapHttpException(
310+
HttpStatusCode.UnprocessableEntity,
311+
"Unprocessable entity",
312+
innerEx
313+
);
314+
315+
// Assert
316+
Assert.IsType<WeaviateUnprocessableEntityException>(result);
317+
}
276318
}

src/Weaviate.Client/Errors.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,42 @@ public WeaviateAuthorizationException(string? message = null, Exception? innerEx
206206
: base(message ?? DefaultMessage, innerException) { }
207207
}
208208

209+
/// <summary>
210+
/// Exception thrown when the server cannot process the request due to a client error (HTTP 400 Bad Request).
211+
/// This can be caused by a malformed request, invalid parameters, or a schema validation error.
212+
/// </summary>
213+
public class WeaviateBadRequestException : WeaviateServerException
214+
{
215+
public const string DefaultMessage =
216+
"The request is invalid. Please check the request parameters and schema.";
217+
218+
public WeaviateBadRequestException(string? message = null, Exception? innerException = null)
219+
: base(message ?? DefaultMessage, innerException) { }
220+
}
221+
222+
/// <summary>
223+
/// Exception thrown when the server understands the content type and syntax of the request,
224+
/// but it is unable to process the contained instructions (HTTP 422 Unprocessable Entity).
225+
/// </summary>
226+
public class WeaviateUnprocessableEntityException : WeaviateServerException
227+
{
228+
public const string DefaultMessage =
229+
"The server is unable to process the request. Please check the request content.";
230+
231+
public WeaviateUnprocessableEntityException(
232+
string? message = null,
233+
Exception? innerException = null
234+
)
235+
: base(message ?? DefaultMessage, innerException) { }
236+
}
237+
209238
/// <summary>
210239
/// Exception thrown when a collection limit has been reached (HTTP 422 Unprocessable Entity).
211240
/// This typically occurs when trying to create more collections than allowed by the server configuration.
212241
/// </summary>
213-
public class WeaviateCollectionLimitReachedException : WeaviateServerException
242+
public class WeaviateCollectionLimitReachedException : WeaviateUnprocessableEntityException
214243
{
215-
public const string DefaultMessage =
244+
public new const string DefaultMessage =
216245
"Collection limit reached. Cannot create more collections than allowed by the server configuration.";
217246

218247
public WeaviateCollectionLimitReachedException(
@@ -226,9 +255,9 @@ public WeaviateCollectionLimitReachedException(
226255
/// Exception thrown when a required module is not available or enabled (HTTP 422 Unprocessable Entity).
227256
/// This occurs when attempting to use a feature that requires a module that is not configured on the server.
228257
/// </summary>
229-
public class WeaviateModuleNotAvailableException : WeaviateServerException
258+
public class WeaviateModuleNotAvailableException : WeaviateUnprocessableEntityException
230259
{
231-
public const string DefaultMessage =
260+
public new const string DefaultMessage =
232261
"Required module is not available or enabled on the Weaviate server. Please check the server's module configuration.";
233262

234263
public WeaviateModuleNotAvailableException(
@@ -253,5 +282,3 @@ public WeaviateExternalModuleProblemException(
253282
)
254283
: base(message ?? DefaultMessage, innerException) { }
255284
}
256-
257-
// TODO WeaviateBadRequestException

src/Weaviate.Client/ExceptionHelper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static WeaviateException MapHttpException(
3434
// Check status code first
3535
WeaviateException? exception = statusCode switch
3636
{
37+
HttpStatusCode.BadRequest => new WeaviateBadRequestException(null, innerException),
3738
HttpStatusCode.Unauthorized => new WeaviateAuthenticationException(
3839
null,
3940
innerException
@@ -68,6 +69,11 @@ public static WeaviateException MapHttpException(
6869
}
6970
}
7071

72+
if (statusCode == HttpStatusCode.UnprocessableEntity)
73+
{
74+
return new WeaviateUnprocessableEntityException(null, innerException);
75+
}
76+
7177
// Re-throw the original exception if we can't map it
7278
throw innerException;
7379
}

src/Weaviate.Client/Rest/Http.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ await response.EnsureExpectedStatusCodeAsync(
6161
new SortedSet<HttpStatusCode>(expectedCodes),
6262
error
6363
);
64-
65-
// TODO
66-
// HttpStatusCode.BadRequest
6764
}
6865
catch (WeaviateUnexpectedStatusCodeException ex)
6966
{

0 commit comments

Comments
 (0)