|
| 1 | +using System.Net; |
| 2 | + |
| 3 | +namespace Weaviate.Client; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Helper class to centralize exception mapping logic for both REST and gRPC APIs. |
| 7 | +/// Provides consistent exception handling across different protocols. |
| 8 | +/// </summary> |
| 9 | +internal static class ExceptionHelper |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Maps an HTTP status code and error message to the appropriate Weaviate exception. |
| 13 | + /// </summary> |
| 14 | + /// <param name="statusCode">The HTTP status code</param> |
| 15 | + /// <param name="errorMessage">The error message from the server</param> |
| 16 | + /// <param name="innerException">The original exception</param> |
| 17 | + /// <param name="resourceType">Optional resource type for context</param> |
| 18 | + /// <returns>The appropriate WeaviateException based on status code and message</returns> |
| 19 | + public static WeaviateException MapHttpException( |
| 20 | + HttpStatusCode statusCode, |
| 21 | + string errorMessage, |
| 22 | + Exception innerException, |
| 23 | + ResourceType resourceType = ResourceType.Unknown |
| 24 | + ) |
| 25 | + { |
| 26 | + // Check status code first |
| 27 | + WeaviateException? exception = statusCode switch |
| 28 | + { |
| 29 | + HttpStatusCode.Unauthorized => new WeaviateAuthenticationException( |
| 30 | + null, |
| 31 | + innerException |
| 32 | + ), |
| 33 | + HttpStatusCode.Forbidden => new WeaviateAuthorizationException(null, innerException), |
| 34 | + HttpStatusCode.NotFound => new WeaviateNotFoundException( |
| 35 | + (Rest.WeaviateUnexpectedStatusCodeException)innerException, |
| 36 | + resourceType |
| 37 | + ), |
| 38 | + HttpStatusCode.Conflict => new WeaviateConflictException( |
| 39 | + $"Conflict accessing {resourceType}", |
| 40 | + innerException |
| 41 | + ), |
| 42 | + _ => null, |
| 43 | + }; |
| 44 | + |
| 45 | + if (exception != null) |
| 46 | + { |
| 47 | + return exception; |
| 48 | + } |
| 49 | + |
| 50 | + // For 422 and 500, try to map based on error message |
| 51 | + if ( |
| 52 | + statusCode == HttpStatusCode.UnprocessableEntity |
| 53 | + || statusCode == HttpStatusCode.InternalServerError |
| 54 | + ) |
| 55 | + { |
| 56 | + var messageBasedException = MapErrorMessage(errorMessage, innerException); |
| 57 | + if (messageBasedException != null) |
| 58 | + { |
| 59 | + return messageBasedException; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Re-throw the original exception if we can't map it |
| 64 | + throw innerException; |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Maps a gRPC RpcException to the appropriate Weaviate exception. |
| 69 | + /// </summary> |
| 70 | + /// <param name="rpcException">The gRPC RpcException</param> |
| 71 | + /// <param name="defaultMessage">The default error message if no specific exception applies</param> |
| 72 | + /// <returns>The appropriate WeaviateException based on gRPC status and message</returns> |
| 73 | + public static WeaviateException MapGrpcException( |
| 74 | + global::Grpc.Core.RpcException rpcException, |
| 75 | + string defaultMessage |
| 76 | + ) |
| 77 | + { |
| 78 | + // Check status code first |
| 79 | + switch (rpcException.StatusCode) |
| 80 | + { |
| 81 | + case global::Grpc.Core.StatusCode.Unauthenticated: |
| 82 | + return new WeaviateAuthenticationException(null, rpcException); |
| 83 | + |
| 84 | + case global::Grpc.Core.StatusCode.PermissionDenied: |
| 85 | + return new WeaviateAuthorizationException(null, rpcException); |
| 86 | + |
| 87 | + case global::Grpc.Core.StatusCode.Unimplemented: |
| 88 | + // This is typically used for feature not supported scenarios |
| 89 | + return new WeaviateFeatureNotSupportedException(null, rpcException); |
| 90 | + } |
| 91 | + |
| 92 | + // Parse error message for specific error types |
| 93 | + var errorMessage = rpcException.Status.Detail ?? rpcException.Message; |
| 94 | + var messageBasedException = MapErrorMessage(errorMessage, rpcException); |
| 95 | + |
| 96 | + if (messageBasedException != null) |
| 97 | + { |
| 98 | + return messageBasedException; |
| 99 | + } |
| 100 | + |
| 101 | + // Default to generic server exception |
| 102 | + return new WeaviateServerException(defaultMessage, rpcException); |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Maps error messages to specific exceptions regardless of protocol. |
| 107 | + /// Returns null if no specific exception type matches. |
| 108 | + /// </summary> |
| 109 | + private static WeaviateException? MapErrorMessage(string errorMessage, Exception innerException) |
| 110 | + { |
| 111 | + if (string.IsNullOrEmpty(errorMessage)) |
| 112 | + { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + // Check for collection limit error |
| 117 | + if ( |
| 118 | + errorMessage.Contains( |
| 119 | + "maximum number of collections", |
| 120 | + StringComparison.OrdinalIgnoreCase |
| 121 | + ) |
| 122 | + ) |
| 123 | + { |
| 124 | + return new WeaviateCollectionLimitReachedException(null, innerException); |
| 125 | + } |
| 126 | + |
| 127 | + // Check for module not available error |
| 128 | + if (errorMessage.Contains("no module with name", StringComparison.OrdinalIgnoreCase)) |
| 129 | + { |
| 130 | + return new WeaviateModuleNotAvailableException(null, innerException); |
| 131 | + } |
| 132 | + |
| 133 | + // Check for vectorization/module execution errors |
| 134 | + if (errorMessage.Contains("could not vectorize", StringComparison.OrdinalIgnoreCase)) |
| 135 | + { |
| 136 | + return new WeaviateExternalModuleProblemException(null, innerException); |
| 137 | + } |
| 138 | + |
| 139 | + return null; |
| 140 | + } |
| 141 | +} |
0 commit comments