|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace OpenClaw.Shared; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Actionable classification of a gateway connection error. Lets the UI route a |
| 7 | +/// raw error string to a specific recovery path instead of a generic failure — |
| 8 | +/// distinguishing unauthorized, scope mismatch, token drift, pairing, TLS, |
| 9 | +/// tunnel, and server problems. |
| 10 | +/// </summary> |
| 11 | +public enum GatewayErrorKind |
| 12 | +{ |
| 13 | + /// <summary>No error text, or nothing recognizable.</summary> |
| 14 | + Unknown, |
| 15 | + |
| 16 | + /// <summary>Connection refused / unreachable / timed out.</summary> |
| 17 | + Network, |
| 18 | + |
| 19 | + /// <summary>Generic unauthorized / invalid-token rejection.</summary> |
| 20 | + Auth, |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// The stored device token is no longer recognized by the gateway (rotated, |
| 24 | + /// revoked, or replaced) — the fix is to re-pair, not to retry. |
| 25 | + /// </summary> |
| 26 | + TokenDrift, |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Authenticated but missing a required operator/node scope (e.g. cannot |
| 30 | + /// approve pairing or read config) — the fix is to re-pair for higher scopes. |
| 31 | + /// </summary> |
| 32 | + ScopeMismatch, |
| 33 | + |
| 34 | + /// <summary>Device/node pairing approval is pending on the gateway host.</summary> |
| 35 | + PairingRequired, |
| 36 | + |
| 37 | + /// <summary>Pairing was explicitly rejected on the gateway host.</summary> |
| 38 | + PairingRejected, |
| 39 | + |
| 40 | + /// <summary>TLS/certificate/cleartext transport problem.</summary> |
| 41 | + Tls, |
| 42 | + |
| 43 | + /// <summary>SSH tunnel could not be established or dropped.</summary> |
| 44 | + Tunnel, |
| 45 | + |
| 46 | + /// <summary>Gateway returned a 5xx / internal error.</summary> |
| 47 | + Server, |
| 48 | + |
| 49 | + /// <summary>Rate limited by the gateway.</summary> |
| 50 | + RateLimited, |
| 51 | +} |
| 52 | + |
| 53 | +/// <summary> |
| 54 | +/// Pure heuristic classifier for gateway error strings. Order is significant: |
| 55 | +/// the more specific kinds (scope, token drift) are matched before the generic |
| 56 | +/// auth bucket so a "re-pair" path wins over a plain "retry" path. |
| 57 | +/// </summary> |
| 58 | +public static class GatewayErrorClassifier |
| 59 | +{ |
| 60 | + public static GatewayErrorKind Classify(string? error) |
| 61 | + { |
| 62 | + if (string.IsNullOrWhiteSpace(error)) |
| 63 | + return GatewayErrorKind.Unknown; |
| 64 | + |
| 65 | + var e = error.ToLowerInvariant(); |
| 66 | + |
| 67 | + if ((Contains(e, "rate") && Contains(e, "limit")) || |
| 68 | + Contains(e, "429") || Contains(e, "too many request")) |
| 69 | + return GatewayErrorKind.RateLimited; |
| 70 | + |
| 71 | + // SSH/tunnel first: SSH failures often read "Permission denied |
| 72 | + // (publickey)" which would otherwise be mistaken for a scope problem. |
| 73 | + if (Contains(e, "ssh") || Contains(e, "tunnel")) |
| 74 | + return GatewayErrorKind.Tunnel; |
| 75 | + |
| 76 | + // Transport security before pairing/auth: e.g. "certificate not |
| 77 | + // approved by CA" must not be read as a pairing approval. |
| 78 | + if (Contains(e, "tls") || Contains(e, "ssl") || Contains(e, "certificate") || |
| 79 | + Contains(e, "cert ") || Contains(e, "handshake") || |
| 80 | + Contains(e, "cleartext") || Contains(e, "insecure")) |
| 81 | + return GatewayErrorKind.Tls; |
| 82 | + |
| 83 | + // Scope/permission problems — authenticated but under-privileged. |
| 84 | + if (Contains(e, "scope") || |
| 85 | + Contains(e, "insufficient priv") || |
| 86 | + Contains(e, "not permitted") || |
| 87 | + Contains(e, "permission denied") || |
| 88 | + (Contains(e, "forbidden") && Contains(e, "scope"))) |
| 89 | + return GatewayErrorKind.ScopeMismatch; |
| 90 | + |
| 91 | + // Token drift — the device token specifically is stale/unknown. |
| 92 | + if (Contains(e, "re-pair") || Contains(e, "repair token") || |
| 93 | + Contains(e, "token rotat") || Contains(e, "token revoked") || |
| 94 | + Contains(e, "token mismatch") || Contains(e, "token drift") || |
| 95 | + (Contains(e, "device token") && |
| 96 | + (Contains(e, "unknown") || Contains(e, "invalid") || |
| 97 | + Contains(e, "expired") || Contains(e, "not recognized") || |
| 98 | + Contains(e, "no longer")))) |
| 99 | + return GatewayErrorKind.TokenDrift; |
| 100 | + |
| 101 | + // Pairing lifecycle. Use specific tokens ("pairing"/"approval") so we |
| 102 | + // don't match "repair" (contains "pair") or "approved by CA". |
| 103 | + if (Contains(e, "pairing") || Contains(e, "approval")) |
| 104 | + { |
| 105 | + if (Contains(e, "reject") || Contains(e, "denied") || Contains(e, "declin")) |
| 106 | + return GatewayErrorKind.PairingRejected; |
| 107 | + return GatewayErrorKind.PairingRequired; |
| 108 | + } |
| 109 | + |
| 110 | + // Server (5xx) before the broad auth bucket: a transient |
| 111 | + // "500 internal error: token validation failed" must not route the |
| 112 | + // user to a re-pair flow. |
| 113 | + if (Contains(e, "500") || Contains(e, "502") || Contains(e, "503") || |
| 114 | + Contains(e, "internal error") || Contains(e, "server error")) |
| 115 | + return GatewayErrorKind.Server; |
| 116 | + |
| 117 | + // Generic auth — after the more specific auth-adjacent kinds above. |
| 118 | + if (Contains(e, "401") || Contains(e, "unauthor") || Contains(e, "forbid") || |
| 119 | + Contains(e, "auth") || Contains(e, "token") || Contains(e, "credential")) |
| 120 | + return GatewayErrorKind.Auth; |
| 121 | + |
| 122 | + // Network. |
| 123 | + if (Contains(e, "refused") || Contains(e, "unreachable") || |
| 124 | + Contains(e, "timeout") || Contains(e, "timed out") || |
| 125 | + Contains(e, "network") || Contains(e, "no route") || |
| 126 | + Contains(e, "could not connect") || Contains(e, "connection closed")) |
| 127 | + return GatewayErrorKind.Network; |
| 128 | + |
| 129 | + return GatewayErrorKind.Unknown; |
| 130 | + } |
| 131 | + |
| 132 | + private static bool Contains(string haystack, string needle) => |
| 133 | + haystack.Contains(needle, StringComparison.Ordinal); |
| 134 | +} |
0 commit comments