Set current error on API call handlers

This commit is contained in:
Angelo Stavrow 2022-05-13 08:33:32 -04:00
parent faa557c2b4
commit 223ebf5b7c
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
5 changed files with 24 additions and 16 deletions

View File

@ -24,7 +24,7 @@ struct AccountView: View {
if let error = model.currentError {
self.errorHandling.handle(error: error)
} else {
self.errorHandling.handle(error: AppError.genericError)
self.errorHandling.handle(error: AppError.genericError(""))
}
model.hasError = false
}

View File

@ -133,7 +133,7 @@ extension LocalStoreError: LocalizedError {
enum AppError: Error {
case couldNotGetLoggedInClient
case couldNotGetPostId
case genericError
case genericError(String)
}
extension AppError: LocalizedError {
@ -143,8 +143,12 @@ extension AppError: LocalizedError {
return NSLocalizedString("Something went wrong trying to access the WriteFreely client.", comment: "")
case .couldNotGetPostId:
return NSLocalizedString("Something went wrong trying to get the post's unique ID.", comment: "")
case .genericError:
return NSLocalizedString("Something went wrong", comment: "")
case .genericError(let customContent):
if customContent.isEmpty {
return NSLocalizedString("Something went wrong", comment: "")
} else {
return NSLocalizedString(customContent, comment: "")
}
}
}
}

View File

@ -44,7 +44,7 @@ extension WriteFreelyModel {
self.posts.purgePublishedPosts()
}
} catch {
print(KeychainError.couldNotPurgeAccessToken.localizedDescription)
self.currentError = KeychainError.couldNotPurgeAccessToken
}
} catch WFError.notFound {
// The user token is invalid or doesn't exist, so it's been invalidated by the server. Proceed with
@ -59,7 +59,7 @@ extension WriteFreelyModel {
self.posts.purgePublishedPosts()
}
} catch {
print(KeychainError.couldNotPurgeAccessToken.localizedDescription)
self.currentError = KeychainError.couldNotPurgeAccessToken
}
} catch {
// We get a 'cannot parse response' (similar to what we were seeing in the Swift package) NSURLError here,
@ -101,7 +101,7 @@ extension WriteFreelyModel {
self.currentError = AccountError.genericAuthError
self.logout()
} catch {
print(error)
self.currentError = AppError.genericError(error.localizedDescription)
}
}
@ -123,7 +123,11 @@ extension WriteFreelyModel {
if let fetchedPostUpdatedDate = fetchedPost.updatedDate,
let localPostUpdatedDate = managedPost.updatedDate {
managedPost.hasNewerRemoteCopy = fetchedPostUpdatedDate > localPostUpdatedDate
} else { print("Error: could not determine which copy of post is newer") }
} else {
self.currentError = AppError.genericError(
"Error updating post: could not determine which copy of post is newer."
)
}
postsToDelete.removeAll(where: { $0.postId == fetchedPost.postId })
}
} else {
@ -140,13 +144,13 @@ extension WriteFreelyModel {
LocalStorageManager.standard.saveContext()
}
} catch {
print(error)
self.currentError = AppError.genericError(error.localizedDescription)
}
} catch WFError.unauthorized {
self.currentError = AccountError.genericAuthError
self.logout()
} catch {
print("Error: Failed to fetch cached posts")
self.currentError = LocalStoreError.couldNotFetchPosts("cached")
}
}
@ -190,11 +194,11 @@ extension WriteFreelyModel {
LocalStorageManager.standard.saveContext()
}
} catch {
print("Error: Failed to fetch cached posts")
self.currentError = LocalStoreError.couldNotFetchPosts("cached")
}
}
} catch {
print(error)
self.currentError = AppError.genericError(error.localizedDescription)
}
}
@ -216,7 +220,7 @@ extension WriteFreelyModel {
LocalStorageManager.standard.saveContext()
}
} catch {
print(error)
self.currentError = AppError.genericError(error.localizedDescription)
}
}
@ -238,7 +242,7 @@ extension WriteFreelyModel {
DispatchQueue.main.async {
LocalStorageManager.standard.container.viewContext.rollback()
}
print(error)
self.currentError = AppError.genericError(error.localizedDescription)
}
}

View File

@ -66,7 +66,7 @@ struct ContentView: View {
if let error = model.currentError {
self.errorHandling.handle(error: error)
} else {
self.errorHandling.handle(error: AppError.genericError)
self.errorHandling.handle(error: AppError.genericError(""))
}
model.hasError = false
}

View File

@ -133,7 +133,7 @@ struct PostListView: View {
if let error = model.currentError {
self.errorHandling.handle(error: error)
} else {
self.errorHandling.handle(error: AppError.genericError)
self.errorHandling.handle(error: AppError.genericError(""))
}
model.hasError = false
}