From 529e1707043a9040199ac6deb4509c55819fb7d8 Mon Sep 17 00:00:00 2001 From: Bob Sun Date: Sat, 4 Feb 2017 14:24:59 +0800 Subject: [PATCH] add error handling --- pass/PasswordStore.swift | 50 +++++++++------------- pass/PasswordsViewController.swift | 16 +++++--- pass/SettingsTableViewController.swift | 57 ++++++++++++++------------ 3 files changed, 61 insertions(+), 62 deletions(-) diff --git a/pass/PasswordStore.swift b/pass/PasswordStore.swift index 99343b5..352fc78 100644 --- a/pass/PasswordStore.swift +++ b/pass/PasswordStore.swift @@ -90,7 +90,7 @@ class PasswordStore { func cloneRepository(remoteRepoURL: URL, credential: GitCredential, transferProgressBlock: @escaping (UnsafePointer, UnsafeMutablePointer) -> Void, - checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) -> Bool { + checkoutProgressBlock: @escaping (String?, UInt, UInt) -> Void) throws { print("start cloning remote repo: \(remoteRepoURL)") let fm = FileManager.default if (storeRepository != nil) { @@ -101,39 +101,27 @@ class PasswordStore { print(error.debugDescription) } } - do { - print("start cloning...") - let credentialProvider = try credential.credentialProvider() - let options: [String: Any] = [ - GTRepositoryCloneOptionsCredentialProvider: credentialProvider, - ] - storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: storeURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock) - print("clone finish") - updatePasswordEntityCoreData() - gitCredential = credential - return true - } catch { - print(error) - return false - } + print("start cloning...") + let credentialProvider = try credential.credentialProvider() + let options: [String: Any] = [ + GTRepositoryCloneOptionsCredentialProvider: credentialProvider, + ] + storeRepository = try GTRepository.clone(from: remoteRepoURL, toWorkingDirectory: storeURL, options: options, transferProgressBlock:transferProgressBlock, checkoutProgressBlock: checkoutProgressBlock) + print("clone finish") + updatePasswordEntityCoreData() + gitCredential = credential } - func pullRepository(transferProgressBlock: @escaping (UnsafePointer, UnsafeMutablePointer) -> Void) -> Bool { + func pullRepository(transferProgressBlock: @escaping (UnsafePointer, UnsafeMutablePointer) -> Void) throws { print("pullRepoisitory") - do { - print("start pulling...") - let credentialProvider = try gitCredential!.credentialProvider() - let options: [String: Any] = [ - GTRepositoryRemoteOptionsCredentialProvider: credentialProvider - ] - let remote = try GTRemote(name: "origin", in: storeRepository!) - try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock) - updatePasswordEntityCoreData() - return true - } catch { - print(error) - return false - } + print("start pulling...") + let credentialProvider = try gitCredential!.credentialProvider() + let options: [String: Any] = [ + GTRepositoryRemoteOptionsCredentialProvider: credentialProvider + ] + let remote = try GTRemote(name: "origin", in: storeRepository!) + try storeRepository?.pull((storeRepository?.currentBranch())!, from: remote, withOptions: options, progress: transferProgressBlock) + updatePasswordEntityCoreData() } func updatePasswordEntityCoreData() { diff --git a/pass/PasswordsViewController.swift b/pass/PasswordsViewController.swift index bfe0da5..707667a 100644 --- a/pass/PasswordsViewController.swift +++ b/pass/PasswordsViewController.swift @@ -34,11 +34,12 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV SVProgressHUD.setDefaultMaskType(.black) SVProgressHUD.show(withStatus: "Sync Passwords") DispatchQueue.global(qos: .userInitiated).async { [unowned self] in - if PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in - DispatchQueue.main.async { - SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository") - } - }) { + do { + try PasswordStore.shared.pullRepository(transferProgressBlock: {(git_transfer_progress, stop) in + DispatchQueue.main.async { + SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Pull Remote Repository") + } + }) DispatchQueue.main.async { SVProgressHUD.showSuccess(withStatus: "Done") SVProgressHUD.dismiss(withDelay: 1) @@ -46,6 +47,11 @@ class PasswordsViewController: UIViewController, UITableViewDataSource, UITableV print("pull success") self.passwordEntities = PasswordStore.shared.fetchPasswordEntityCoreData() self.reloadTableView(data: self.passwordEntities!) + } catch { + DispatchQueue.main.async { + SVProgressHUD.showError(withStatus: error.localizedDescription) + SVProgressHUD.dismiss(withDelay: 3) + } } } } diff --git a/pass/SettingsTableViewController.swift b/pass/SettingsTableViewController.swift index b7f6145..7da381b 100644 --- a/pass/SettingsTableViewController.swift +++ b/pass/SettingsTableViewController.swift @@ -38,35 +38,40 @@ class SettingsTableViewController: UITableViewController { } DispatchQueue.global(qos: .userInitiated).async { - let ret = PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!, - credential: gitCredential, - transferProgressBlock:{ (git_transfer_progress, stop) in - DispatchQueue.main.async { - SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository") - } - }, - checkoutProgressBlock: { (path, completedSteps, totalSteps) in - DispatchQueue.main.async { - SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch") - } - }) - - DispatchQueue.main.async { - if ret { - SVProgressHUD.showSuccess(withStatus: "Done") - SVProgressHUD.dismiss(withDelay: 1) - - Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL) - Defaults[.gitRepositoryUsername] = username - Defaults[.gitRepositoryPassword] = password - Defaults[.gitRepositoryAuthenticationMethod] = auth + do { + try PasswordStore.shared.cloneRepository(remoteRepoURL: URL(string: gitRepostiroyURL)!, + credential: gitCredential, + transferProgressBlock:{ (git_transfer_progress, stop) in + DispatchQueue.main.async { + SVProgressHUD.showProgress(Float(git_transfer_progress.pointee.received_objects)/Float(git_transfer_progress.pointee.total_objects), status: "Clone Remote Repository") + } + }, + checkoutProgressBlock: { (path, completedSteps, totalSteps) in + DispatchQueue.main.async { + SVProgressHUD.showProgress(Float(completedSteps)/Float(totalSteps), status: "Checkout Master Branch") + } + }) + + DispatchQueue.main.async { + SVProgressHUD.showSuccess(withStatus: "Done") + SVProgressHUD.dismiss(withDelay: 1) + + Defaults[.gitRepositoryURL] = URL(string: gitRepostiroyURL) + Defaults[.gitRepositoryUsername] = username + Defaults[.gitRepositoryPassword] = password + Defaults[.gitRepositoryAuthenticationMethod] = auth + + NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) - NotificationCenter.default.post(Notification(name: Notification.Name("passwordUpdated"))) - } else { - SVProgressHUD.showError(withStatus: "Error") - SVProgressHUD.dismiss(withDelay: 1) + } + } catch { + DispatchQueue.main.async { + print(error) + SVProgressHUD.showError(withStatus: error.localizedDescription) + SVProgressHUD.dismiss(withDelay: 3) } } + } } } else if let controller = segue.source as? PGPKeySettingTableViewController {