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 { if let error = model.currentError {
self.errorHandling.handle(error: error) self.errorHandling.handle(error: error)
} else { } else {
self.errorHandling.handle(error: AppError.genericError) self.errorHandling.handle(error: AppError.genericError(""))
} }
model.hasError = false model.hasError = false
} }

View File

@ -133,7 +133,7 @@ extension LocalStoreError: LocalizedError {
enum AppError: Error { enum AppError: Error {
case couldNotGetLoggedInClient case couldNotGetLoggedInClient
case couldNotGetPostId case couldNotGetPostId
case genericError case genericError(String)
} }
extension AppError: LocalizedError { extension AppError: LocalizedError {
@ -143,8 +143,12 @@ extension AppError: LocalizedError {
return NSLocalizedString("Something went wrong trying to access the WriteFreely client.", comment: "") return NSLocalizedString("Something went wrong trying to access the WriteFreely client.", comment: "")
case .couldNotGetPostId: case .couldNotGetPostId:
return NSLocalizedString("Something went wrong trying to get the post's unique ID.", comment: "") return NSLocalizedString("Something went wrong trying to get the post's unique ID.", comment: "")
case .genericError: case .genericError(let customContent):
return NSLocalizedString("Something went wrong", comment: "") 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() self.posts.purgePublishedPosts()
} }
} catch { } catch {
print(KeychainError.couldNotPurgeAccessToken.localizedDescription) self.currentError = KeychainError.couldNotPurgeAccessToken
} }
} catch WFError.notFound { } catch WFError.notFound {
// The user token is invalid or doesn't exist, so it's been invalidated by the server. Proceed with // 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() self.posts.purgePublishedPosts()
} }
} catch { } catch {
print(KeychainError.couldNotPurgeAccessToken.localizedDescription) self.currentError = KeychainError.couldNotPurgeAccessToken
} }
} catch { } catch {
// We get a 'cannot parse response' (similar to what we were seeing in the Swift package) NSURLError here, // 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.currentError = AccountError.genericAuthError
self.logout() self.logout()
} catch { } catch {
print(error) self.currentError = AppError.genericError(error.localizedDescription)
} }
} }
@ -123,7 +123,11 @@ extension WriteFreelyModel {
if let fetchedPostUpdatedDate = fetchedPost.updatedDate, if let fetchedPostUpdatedDate = fetchedPost.updatedDate,
let localPostUpdatedDate = managedPost.updatedDate { let localPostUpdatedDate = managedPost.updatedDate {
managedPost.hasNewerRemoteCopy = fetchedPostUpdatedDate > localPostUpdatedDate 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 }) postsToDelete.removeAll(where: { $0.postId == fetchedPost.postId })
} }
} else { } else {
@ -140,13 +144,13 @@ extension WriteFreelyModel {
LocalStorageManager.standard.saveContext() LocalStorageManager.standard.saveContext()
} }
} catch { } catch {
print(error) self.currentError = AppError.genericError(error.localizedDescription)
} }
} catch WFError.unauthorized { } catch WFError.unauthorized {
self.currentError = AccountError.genericAuthError self.currentError = AccountError.genericAuthError
self.logout() self.logout()
} catch { } catch {
print("Error: Failed to fetch cached posts") self.currentError = LocalStoreError.couldNotFetchPosts("cached")
} }
} }
@ -190,11 +194,11 @@ extension WriteFreelyModel {
LocalStorageManager.standard.saveContext() LocalStorageManager.standard.saveContext()
} }
} catch { } catch {
print("Error: Failed to fetch cached posts") self.currentError = LocalStoreError.couldNotFetchPosts("cached")
} }
} }
} catch { } catch {
print(error) self.currentError = AppError.genericError(error.localizedDescription)
} }
} }
@ -216,7 +220,7 @@ extension WriteFreelyModel {
LocalStorageManager.standard.saveContext() LocalStorageManager.standard.saveContext()
} }
} catch { } catch {
print(error) self.currentError = AppError.genericError(error.localizedDescription)
} }
} }
@ -238,7 +242,7 @@ extension WriteFreelyModel {
DispatchQueue.main.async { DispatchQueue.main.async {
LocalStorageManager.standard.container.viewContext.rollback() 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 { if let error = model.currentError {
self.errorHandling.handle(error: error) self.errorHandling.handle(error: error)
} else { } else {
self.errorHandling.handle(error: AppError.genericError) self.errorHandling.handle(error: AppError.genericError(""))
} }
model.hasError = false model.hasError = false
} }

View File

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