feat: global API request/response logging in APIClient
Every API call now logs: method, path, statusCode, response preview. Network errors caught and logged before re-throwing. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
78d2c93b41
commit
5a7ee670af
Binary file not shown.
@ -56,20 +56,24 @@ actor APIClient {
|
||||
}
|
||||
|
||||
if let body {
|
||||
let encoded = try JSONEncoder().encode(AnyEncodable(body))
|
||||
request.httpBody = encoded
|
||||
print("[APIClient] \(method) \(path) bodySize=\(encoded.count)")
|
||||
} else {
|
||||
print("[APIClient] \(method) \(path)")
|
||||
request.httpBody = try JSONEncoder().encode(AnyEncodable(body))
|
||||
}
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
print("[APIClient] \(method) \(path) -> response received")
|
||||
let (data, response): (Data, URLResponse)
|
||||
do {
|
||||
(data, response) = try await session.data(for: request)
|
||||
} catch {
|
||||
print("[API] \(method) \(path) ❌ \(error.localizedDescription)")
|
||||
throw APIError.networkError(error)
|
||||
}
|
||||
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
throw APIError.networkError(NSError(domain: "", code: -1))
|
||||
}
|
||||
|
||||
let responseBody = String(data: data, encoding: .utf8)?.prefix(300) ?? ""
|
||||
print("[API] \(method) \(path) → \(httpResponse.statusCode) | \(responseBody)")
|
||||
|
||||
switch httpResponse.statusCode {
|
||||
case 200, 201:
|
||||
return try decodeResponse(data)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user