Merge pull request #1204 from MiguelMachado-dev/fix/zai-body-auth-errors

fix(mac): map Z.ai body authentication errors
This commit is contained in:
Resham Joshi 2026-08-31 09:59:49 -07:00 • committed by GitHub
commit f54329e686
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View file

@ -110,6 +110,10 @@ enum ZaiSubscriptionService {
guard let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
throw FetchError.parseFailure
}
let bodyCode = jsonNumber(root["code"]).map(Int.init)
if bodyCode == 401 || bodyCode == 403 { throw FetchError.authenticationRejected }
if root["success"] as? Bool == false { throw FetchError.parseFailure }
let payload = (root["data"] as? [String: Any]) ?? root
guard let limits = payload["limits"] as? [Any] else { throw FetchError.parseFailure }

View file

@ -123,10 +123,28 @@ struct ZaiQuotaTests {
}
}
@Test("HTTP 200 body authentication failures are terminal")
func bodyAuthenticationFailure() async throws {
for code in [401, 403] {
let recorder = RequestRecorder()
let body = #"{"code":\#(code),"msg":"token expired or incorrect","success":false}"#
do {
_ = try await ZaiSubscriptionService.refresh(
apiKey: Self.syntheticKey,
deps: Self.deps(recorder: recorder, body: body)
)
Issue.record("Expected body code \(code) to reject authentication")
} catch let error as ZaiSubscriptionService.FetchError {
#expect(error == .authenticationRejected)
}
}
}
@Test("malformed or empty quota fails")
func malformedQuota() throws {
for body in [
"not json",
#"{"code":500,"success":false}"#,
#"{"data":{}}"#,
#"{"data":{"limits":[]}}"#,
#"{"data":{"limits":[{"type":"CREDIT_LIMIT","unit":3,"number":5}]}}"#,