Adds more failed api calls and improves error message

multistore
Laurent 1 year ago
parent d1c33995c9
commit a4bbe86e34
  1. 14
      LeStorage/Services.swift
  2. 21
      LeStorage/Store.swift

@ -116,6 +116,8 @@ public class Services {
if let apiCallId, let type = (T.self as? any Storable.Type) {
try Store.main.rescheduleApiCall(id: apiCallId, type: type)
Store.main.logFailedAPICall(apiCallId, collectionName: type.resourceName(), error: errorString)
} else {
Store.main.logFailedAPICall(request: request, error: errorString)
}
throw ServiceError.responseError(response: errorString)
@ -357,10 +359,14 @@ public class Services {
fileprivate func errorMessageFromResponse(data: Data) -> String? {
do {
if let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
if let stringsArray = jsonObject.values.first as? [String] {
return stringsArray.first
} else if let string = jsonObject.values.first as? String {
return string
if let tuple = jsonObject.first {
var error = ""
if let stringsArray = tuple.value as? [String], let first = stringsArray.first {
error = first
} else if let string = tuple.value as? String {
error = string
}
return "\(error) (\(tuple.key))"
}
}
} catch {

@ -282,11 +282,12 @@ public class Store {
guard let failedAPICallsCollection = self._failedAPICallsCollection,
let collection = self._collections[collectionName],
collectionName != FailedAPICall.resourceName(),
let apiCall = try? collection.apiCallById(apiCallId) else {
return
}
if !failedAPICallsCollection.contains(where: { $0.callId == apiCallId }) && apiCall.attemptsCount > 5 {
if !failedAPICallsCollection.contains(where: { $0.callId == apiCallId }) && apiCall.attemptsCount > 6 {
do {
let string = try apiCall.jsonString()
@ -299,4 +300,22 @@ public class Store {
}
func logFailedAPICall(request: URLRequest, error: String) {
guard let failedAPICallsCollection = self._failedAPICallsCollection,
let body: Data = request.httpBody,
let bodyString = String(data: body, encoding: .utf8),
let url = request.url?.absoluteString else {
return
}
do {
let failedAPICall = FailedAPICall(callId: request.hashValue.formatted(), type: url, apiCall: bodyString, error: error)
try failedAPICallsCollection.addOrUpdate(instance: failedAPICall)
} catch {
Logger.error(error)
}
}
}

Loading…
Cancel
Save