|
| 1 | +using System.ClientModel; |
| 2 | +using System.Globalization; |
| 3 | +using System.Net; |
| 4 | +using System.Reflection; |
| 5 | +using SharpClaw.Code.Protocol.Models; |
| 6 | + |
| 7 | +namespace SharpClaw.Code.Providers.Models; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Classifies provider stream failures without changing the provider event contract. |
| 11 | +/// </summary> |
| 12 | +public static class ProviderStreamFailureClassifier |
| 13 | +{ |
| 14 | + private static readonly string[] StatusPropertyNames = ["StatusCode", "Status"]; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Returns whether an authentication status is already expired. |
| 18 | + /// </summary> |
| 19 | + public static bool IsExpired(AuthStatus authStatus, DateTimeOffset utcNow) |
| 20 | + => authStatus.ExpiresAtUtc is { } expiresAt && expiresAt <= utcNow; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Converts an exception into stable failed-event content while preserving HTTP status detail when available. |
| 24 | + /// </summary> |
| 25 | + public static string Describe(Exception exception) |
| 26 | + { |
| 27 | + var message = string.IsNullOrWhiteSpace(exception.Message) |
| 28 | + ? exception.GetType().Name |
| 29 | + : exception.Message; |
| 30 | + var statusCode = TryGetStatusCode(exception); |
| 31 | + return statusCode is null |
| 32 | + ? message |
| 33 | + : $"HTTP {(int)statusCode.Value} {statusCode.Value}: {message}"; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Classifies a terminal provider failed event. |
| 38 | + /// </summary> |
| 39 | + public static ProviderFailureKind ClassifyFailedEvent(ProviderEvent providerEvent) |
| 40 | + => LooksLikeAuthenticationFailure(providerEvent.Content) |
| 41 | + ? ProviderFailureKind.AuthenticationUnavailable |
| 42 | + : ProviderFailureKind.StreamFailed; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Returns whether an exception represents provider authentication or authorization failure. |
| 46 | + /// </summary> |
| 47 | + public static bool IsAuthenticationFailure(Exception exception) |
| 48 | + { |
| 49 | + var statusCode = TryGetStatusCode(exception); |
| 50 | + if (statusCode is HttpStatusCode.Unauthorized or HttpStatusCode.Forbidden) |
| 51 | + { |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + var typeName = exception.GetType().Name; |
| 56 | + return typeName.Contains("Unauthorized", StringComparison.OrdinalIgnoreCase) |
| 57 | + || typeName.Contains("Forbidden", StringComparison.OrdinalIgnoreCase) |
| 58 | + || typeName.Contains("Authentication", StringComparison.OrdinalIgnoreCase) |
| 59 | + || LooksLikeAuthenticationFailure(exception.Message) |
| 60 | + || (exception.InnerException is not null && IsAuthenticationFailure(exception.InnerException)); |
| 61 | + } |
| 62 | + |
| 63 | + private static bool LooksLikeAuthenticationFailure(string? message) |
| 64 | + { |
| 65 | + if (string.IsNullOrWhiteSpace(message)) |
| 66 | + { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + var value = message.ToLowerInvariant(); |
| 71 | + if (value.Contains("401", StringComparison.Ordinal) |
| 72 | + || value.Contains("unauthorized", StringComparison.Ordinal) |
| 73 | + || value.Contains("forbidden", StringComparison.Ordinal)) |
| 74 | + { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + if ((value.Contains("api key", StringComparison.Ordinal) |
| 79 | + || value.Contains("token", StringComparison.Ordinal) |
| 80 | + || value.Contains("credential", StringComparison.Ordinal) |
| 81 | + || value.Contains("authentication", StringComparison.Ordinal)) |
| 82 | + && (value.Contains("expired", StringComparison.Ordinal) |
| 83 | + || value.Contains("invalid", StringComparison.Ordinal) |
| 84 | + || value.Contains("missing", StringComparison.Ordinal) |
| 85 | + || value.Contains("failed", StringComparison.Ordinal))) |
| 86 | + { |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + private static HttpStatusCode? TryGetStatusCode(Exception exception) |
| 94 | + { |
| 95 | + if (exception is HttpRequestException { StatusCode: { } httpStatusCode }) |
| 96 | + { |
| 97 | + return httpStatusCode; |
| 98 | + } |
| 99 | + |
| 100 | + if (exception is ClientResultException { Status: > 0 } clientResultException) |
| 101 | + { |
| 102 | + return (HttpStatusCode)clientResultException.Status; |
| 103 | + } |
| 104 | + |
| 105 | + var reflected = TryGetReflectedStatusCode(exception); |
| 106 | + if (reflected is not null) |
| 107 | + { |
| 108 | + return reflected; |
| 109 | + } |
| 110 | + |
| 111 | + return exception.InnerException is null ? null : TryGetStatusCode(exception.InnerException); |
| 112 | + } |
| 113 | + |
| 114 | + private static HttpStatusCode? TryGetReflectedStatusCode(Exception exception) |
| 115 | + { |
| 116 | + foreach (var propertyName in StatusPropertyNames) |
| 117 | + { |
| 118 | + var property = exception.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public); |
| 119 | + if (property?.GetValue(exception) is not { } value) |
| 120 | + { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + if (value is HttpStatusCode httpStatusCode) |
| 125 | + { |
| 126 | + return httpStatusCode; |
| 127 | + } |
| 128 | + |
| 129 | + if (value is int intStatusCode and > 0) |
| 130 | + { |
| 131 | + return (HttpStatusCode)intStatusCode; |
| 132 | + } |
| 133 | + |
| 134 | + if (value.GetType().IsEnum) |
| 135 | + { |
| 136 | + var enumStatusCode = Convert.ToInt32(value, CultureInfo.InvariantCulture); |
| 137 | + if (enumStatusCode > 0) |
| 138 | + { |
| 139 | + return (HttpStatusCode)enumStatusCode; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | +} |
0 commit comments