Fix CLI v2 commands showing JSON parse error instead of actual error (#189)

When the server returns a plain-text error (e.g., "ERROR: Access denied
...") before the JSON protocol starts, sendV2() would pass it through
JSONSerialization which throws a confusing NSCocoaErrorDomain 3840 error.

Now sendV2() checks for "ERROR:" prefix and surfaces the real message.
Also includes the raw response in the fallback error for easier debugging.

Fixes https://github.com/manaflow-ai/cmux/issues/188
This commit is contained in:
Lawrence Chen 2026-02-20 14:53:00 -08:00 committed by GitHub
parent f455af4541
commit 327d658069
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -365,11 +365,19 @@ final class SocketClient {
}
let raw = try send(command: requestLine)
// The server may return plain-text errors (e.g., "ERROR: Access denied ...")
// before the JSON protocol starts. Surface these directly instead of letting
// JSONSerialization throw a confusing parse error.
if raw.hasPrefix("ERROR:") {
throw CLIError(message: raw)
}
guard let responseData = raw.data(using: .utf8) else {
throw CLIError(message: "Invalid UTF-8 v2 response")
}
guard let response = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any] else {
throw CLIError(message: "Invalid v2 response")
throw CLIError(message: "Invalid v2 response: \(raw)")
}
if let ok = response["ok"] as? Bool, ok {