payment and list
This commit is contained in:
@@ -13,6 +13,7 @@ enum NetworkError: Error, LocalizedError {
|
||||
case unauthorized(String?)
|
||||
case decodeError(String?)
|
||||
case rateLimited(Int?)
|
||||
case cancelled
|
||||
case transportError(String)
|
||||
|
||||
var errorDescription: String? {
|
||||
@@ -33,6 +34,8 @@ enum NetworkError: Error, LocalizedError {
|
||||
return "Muitas requisicoes. Tente novamente em \(retryAfter)s."
|
||||
}
|
||||
return "Muitas requisicoes. Tente novamente."
|
||||
case .cancelled:
|
||||
return "Requisicao cancelada"
|
||||
case .transportError(let message):
|
||||
return "Erro de rede: \(message)"
|
||||
}
|
||||
@@ -82,7 +85,11 @@ final class ApiClient {
|
||||
// NOTE:
|
||||
// /api/customer/login is strict about body fields (email/phoneNumber/otp).
|
||||
// On iOS, routing this endpoint through URLSession ensures JSON body arrives as-is.
|
||||
if request.path == "/api/customer/login" {
|
||||
// Also route order-related polling/listing through URLSession to avoid
|
||||
// intermittent cancellation observed with the shared API bridge.
|
||||
if request.path == "/api/customer/login" ||
|
||||
request.path == "/api/app/orders" ||
|
||||
request.path.hasPrefix("/api/public/orders/") {
|
||||
if let body = request.body, let bodyText = String(data: body, encoding: .utf8) {
|
||||
print("[ApiClient] /api/customer/login body: \(bodyText)")
|
||||
}
|
||||
@@ -161,7 +168,6 @@ private extension ApiClient {
|
||||
}
|
||||
|
||||
func mapError(_ error: Error) -> NetworkError {
|
||||
printError(title: "httpReqError", msg: error.localizedDescription)
|
||||
if let network = error as? NetworkError {
|
||||
return network
|
||||
}
|
||||
@@ -176,6 +182,12 @@ private extension ApiClient {
|
||||
return .unauthorized(payload?.message ?? apiMessage)
|
||||
}
|
||||
|
||||
if nsError.domain == NSURLErrorDomain && nsError.code == NSURLErrorCancelled {
|
||||
return .cancelled
|
||||
}
|
||||
|
||||
printError(title: "httpReqError", msg: error.localizedDescription)
|
||||
|
||||
switch nsError.code {
|
||||
case 401, 403:
|
||||
return .unauthorized(apiMessage)
|
||||
@@ -209,6 +221,8 @@ private extension ApiClient {
|
||||
while attempt <= maxAttempts {
|
||||
do {
|
||||
return try await perform(urlRequest, as: T.self)
|
||||
} catch is CancellationError {
|
||||
throw NetworkError.cancelled
|
||||
} catch let error as NetworkError {
|
||||
guard shouldRetry(error), attempt < maxAttempts else {
|
||||
throw error
|
||||
@@ -230,6 +244,9 @@ private extension ApiClient {
|
||||
do {
|
||||
(data, response) = try await session.data(for: request)
|
||||
} catch {
|
||||
if let urlError = error as? URLError, urlError.code == .cancelled {
|
||||
throw NetworkError.cancelled
|
||||
}
|
||||
throw NetworkError.transportError(error.localizedDescription)
|
||||
}
|
||||
|
||||
@@ -283,7 +300,7 @@ private extension ApiClient {
|
||||
return true
|
||||
case .httpError(let statusCode, _):
|
||||
return statusCode >= 500
|
||||
case .invalidURL, .invalidResponse, .decodeError, .unauthorized:
|
||||
case .invalidURL, .invalidResponse, .decodeError, .unauthorized, .cancelled:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user