From 223ebf5b7cc1e34e98cc65b980673cbfdbbe527c Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Fri, 13 May 2022 08:33:32 -0400 Subject: [PATCH] Set current error on API call handlers --- Shared/Account/AccountView.swift | 2 +- Shared/ErrorHandling/ErrorConstants.swift | 10 +++++--- .../WriteFreelyModel+APIHandlers.swift | 24 +++++++++++-------- Shared/Navigation/ContentView.swift | 2 +- Shared/PostList/PostListView.swift | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Shared/Account/AccountView.swift b/Shared/Account/AccountView.swift index 5843048..fe22f19 100644 --- a/Shared/Account/AccountView.swift +++ b/Shared/Account/AccountView.swift @@ -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 } diff --git a/Shared/ErrorHandling/ErrorConstants.swift b/Shared/ErrorHandling/ErrorConstants.swift index eb1f037..2962246 100644 --- a/Shared/ErrorHandling/ErrorConstants.swift +++ b/Shared/ErrorHandling/ErrorConstants.swift @@ -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: "") + } } } } diff --git a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift index 6d0d147..c1d19d9 100644 --- a/Shared/Extensions/WriteFreelyModel+APIHandlers.swift +++ b/Shared/Extensions/WriteFreelyModel+APIHandlers.swift @@ -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) } } diff --git a/Shared/Navigation/ContentView.swift b/Shared/Navigation/ContentView.swift index f1e82a0..ca13ff5 100644 --- a/Shared/Navigation/ContentView.swift +++ b/Shared/Navigation/ContentView.swift @@ -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 } diff --git a/Shared/PostList/PostListView.swift b/Shared/PostList/PostListView.swift index 2158aa4..16e1f45 100644 --- a/Shared/PostList/PostListView.swift +++ b/Shared/PostList/PostListView.swift @@ -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 }