mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-23 13:29:08 +00:00
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:
commit
f54329e686
2 changed files with 22 additions and 0 deletions
|
|
@ -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 }
|
||||
|
||||
|
|
|
|||
|
|
@ -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}]}}"#,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue