Add missing error messages and name them consistently

Since the AppError enum is suffixed with 'Error', the elements itself do not need this suffix, too.
This commit is contained in:
Danny Moesch 2019-01-20 13:08:29 +01:00 committed by Mingshen Sun
parent 08c91599b6
commit 38b44cedf8
3 changed files with 39 additions and 34 deletions

View file

@ -54,12 +54,13 @@
"FailedToDeletePasswordEntity" = "Failed to delete password entity: %@"; "FailedToDeletePasswordEntity" = "Failed to delete password entity: %@";
"FailedToSavePasswordEntity" = "Failed to save password entity: %@"; "FailedToSavePasswordEntity" = "Failed to save password entity: %@";
"FailureToSaveContext" = "Failure to save context: %@"; "FailureToSaveContext" = "Failure to save context: %@";
"RepositoryRemoteMasterNotFoundError." = "Cannot find remote branch origin/master."; "RepositoryRemoteBranchNotFoundError." = "Cannot find remote branch %@.";
"RepositoryBranchNotFoundError." = "Branch %@ not found in repository.";
"KeyImportError." = "Cannot import the key."; "KeyImportError." = "Cannot import the key.";
"PasswordDuplicatedError." = "Cannot add the password; password is duplicated."; "PasswordDuplicatedError." = "Cannot add the password; password is duplicated.";
"GitResetError." = "Cannot identify the latest synced commit."; "GitResetError." = "Cannot identify the latest synced commit.";
"PGPPublicKeyNotExistError." = "PGP public key doesn't exist."; "PgpPublicKeyNotExistError." = "PGP public key doesn't exist.";
"WrongPasswordFilename." = "Cannot write to the password file."; "WrongPasswordFilenameError." = "Cannot write to the password file.";
"DecryptionError." = "Cannot decrypt password."; "DecryptionError." = "Cannot decrypt password.";
"UnknownError." = "Unknown error."; "UnknownError." = "Unknown error.";
"PrepareRepository" = "Prepare Repository"; "PrepareRepository" = "Prepare Repository";

View file

@ -6,23 +6,27 @@
// Copyright © 2017 Bob Sun. All rights reserved. // Copyright © 2017 Bob Sun. All rights reserved.
// //
import Foundation
public enum AppError: Error { public enum AppError: Error {
case RepositoryNotSetError case RepositoryNotSet
case RepositoryRemoteBranchNotFoundError(_: String) case RepositoryRemoteBranchNotFound(_: String)
case RepositoryBranchNotFound(_: String) case RepositoryBranchNotFound(_: String)
case KeyImportError case KeyImport
case PasswordDuplicatedError case PasswordDuplicated
case GitResetError case GitReset
case PGPPublicKeyNotExistError case PgpPublicKeyNotExist
case WrongPasswordFilename case WrongPasswordFilename
case DecryptionError case Decryption
case UnknownError case Unknown
} }
extension AppError: LocalizedError { extension AppError: LocalizedError {
public var errorDescription: String? { public var errorDescription: String? {
return String(describing: self).localize() let localizationKey = "\(String(describing: self).prefix(while: { $0 != "(" }))Error."
switch self {
case let .RepositoryRemoteBranchNotFound(name), let .RepositoryBranchNotFound(name):
return localizationKey.localize(name)
default:
return localizationKey.localize()
}
} }
} }

View file

@ -198,16 +198,16 @@ public class PasswordStore {
let keyPath = Globals.pgpPublicKeyPath let keyPath = Globals.pgpPublicKeyPath
self.publicKey = importKey(from: keyPath) self.publicKey = importKey(from: keyPath)
if self.publicKey == nil { if self.publicKey == nil {
throw AppError.KeyImportError throw AppError.KeyImport
} }
case .secret: case .secret:
let keyPath = Globals.pgpPrivateKeyPath let keyPath = Globals.pgpPrivateKeyPath
self.privateKey = importKey(from: keyPath) self.privateKey = importKey(from: keyPath)
if self.privateKey == nil { if self.privateKey == nil {
throw AppError.KeyImportError throw AppError.KeyImport
} }
default: default:
throw AppError.UnknownError throw AppError.Unknown
} }
} }
@ -335,14 +335,14 @@ public class PasswordStore {
return return
} }
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
let remoteBranchName = "origin/\(localBranchName)" let remoteBranchName = "origin/\(localBranchName)"
guard let remoteBranch = try? storeRepository.lookUpBranch(withName: remoteBranchName, type: .remote, success: nil) else { guard let remoteBranch = try? storeRepository.lookUpBranch(withName: remoteBranchName, type: .remote, success: nil) else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName) throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
} }
guard let remoteBranchOid = remoteBranch.oid else { guard let remoteBranchOid = remoteBranch.oid else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName) throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
} }
let localBranch = try storeRepository.createBranchNamed(localBranchName, from: remoteBranchOid, message: nil) let localBranch = try storeRepository.createBranchNamed(localBranchName, from: remoteBranchOid, message: nil)
try localBranch.updateTrackingBranch(remoteBranch) try localBranch.updateTrackingBranch(remoteBranch)
@ -353,7 +353,7 @@ public class PasswordStore {
public func pullRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws { public func pullRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UnsafePointer<git_transfer_progress>, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword) let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider] let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
@ -519,7 +519,7 @@ public class PasswordStore {
private func gitAdd(path: String) throws { private func gitAdd(path: String) throws {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
try storeRepository.index().addFile(path) try storeRepository.index().addFile(path)
try storeRepository.index().write() try storeRepository.index().write()
@ -527,7 +527,7 @@ public class PasswordStore {
private func gitRm(path: String) throws { private func gitRm(path: String) throws {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
let url = storeURL.appendingPathComponent(path) let url = storeURL.appendingPathComponent(path)
if fm.fileExists(atPath: url.path) { if fm.fileExists(atPath: url.path) {
@ -562,7 +562,7 @@ public class PasswordStore {
private func gitCommit(message: String) throws -> GTCommit? { private func gitCommit(message: String) throws -> GTCommit? {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
let newTree = try storeRepository.index().writeTree() let newTree = try storeRepository.index().writeTree()
let headReference = try storeRepository.headReference() let headReference = try storeRepository.headReference()
@ -576,7 +576,7 @@ public class PasswordStore {
private func getLocalBranch(withName branchName: String) throws -> GTBranch? { private func getLocalBranch(withName branchName: String) throws -> GTBranch? {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
let reference = GTBranch.localNamePrefix().appending(branchName) let reference = GTBranch.localNamePrefix().appending(branchName)
let branches = try storeRepository.branches(withPrefix: reference) let branches = try storeRepository.branches(withPrefix: reference)
@ -585,7 +585,7 @@ public class PasswordStore {
public func pushRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws { public func pushRepository(credential: GitCredential, requestGitPassword: @escaping (GitCredential.Credential, String?) -> String?, transferProgressBlock: @escaping (UInt32, UInt32, Int, UnsafeMutablePointer<ObjCBool>) -> Void) throws {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
do { do {
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword) let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
@ -601,7 +601,7 @@ public class PasswordStore {
private func addPasswordEntities(password: Password) throws -> PasswordEntity? { private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
guard !passwordExisted(password: password) else { guard !passwordExisted(password: password) else {
throw AppError.PasswordDuplicatedError throw AppError.PasswordDuplicated
} }
var passwordURL = password.url var passwordURL = password.url
@ -784,7 +784,7 @@ public class PasswordStore {
// return the number of discarded commits // return the number of discarded commits
public func reset() throws -> Int { public func reset() throws -> Int {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
// get a list of local commits // get a list of local commits
if let localCommits = try getLocalCommits(), if let localCommits = try getLocalCommits(),
@ -793,7 +793,7 @@ public class PasswordStore {
guard let firstLocalCommit = localCommits.last, guard let firstLocalCommit = localCommits.last,
firstLocalCommit.parents.count == 1, firstLocalCommit.parents.count == 1,
let newHead = firstLocalCommit.parents.first else { let newHead = firstLocalCommit.parents.first else {
throw AppError.GitResetError throw AppError.GitReset
} }
try storeRepository.reset(to: newHead, resetType: .hard) try storeRepository.reset(to: newHead, resetType: .hard)
self.setAllSynced() self.setAllSynced()
@ -810,16 +810,16 @@ public class PasswordStore {
private func getLocalCommits() throws -> [GTCommit]? { private func getLocalCommits() throws -> [GTCommit]? {
guard let storeRepository = storeRepository else { guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError throw AppError.RepositoryNotSet
} }
// get the remote branch // get the remote branch
let remoteBranchName = SharedDefaults[.gitBranchName]! let remoteBranchName = SharedDefaults[.gitBranchName]!
guard let remoteBranch = try storeRepository.remoteBranches().first(where: { $0.shortName == remoteBranchName }) else { guard let remoteBranch = try storeRepository.remoteBranches().first(where: { $0.shortName == remoteBranchName }) else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName) throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
} }
// check oid before calling localCommitsRelative // check oid before calling localCommitsRelative
guard remoteBranch.oid != nil else { guard remoteBranch.oid != nil else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName) throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
} }
// get a list of local commits // get a list of local commits
@ -838,14 +838,14 @@ public class PasswordStore {
let decryptedData = try ObjectivePGP.decrypt(encryptedData, andVerifySignature: false, using: keyring.keys, passphraseForKey: {(_) in passphrase}) let decryptedData = try ObjectivePGP.decrypt(encryptedData, andVerifySignature: false, using: keyring.keys, passphraseForKey: {(_) in passphrase})
let plainText = String(data: decryptedData, encoding: .utf8) ?? "" let plainText = String(data: decryptedData, encoding: .utf8) ?? ""
guard let url = passwordEntity.getURL() else { guard let url = passwordEntity.getURL() else {
throw AppError.DecryptionError throw AppError.Decryption
} }
return Password(name: passwordEntity.getName(), url: url, plainText: plainText) return Password(name: passwordEntity.getName(), url: url, plainText: plainText)
} }
public func encrypt(password: Password) throws -> Data { public func encrypt(password: Password) throws -> Data {
guard keyring.keys.count > 0 else { guard keyring.keys.count > 0 else {
throw AppError.PGPPublicKeyNotExistError throw AppError.PgpPublicKeyNotExist
} }
let plainData = password.plainData let plainData = password.plainData
let encryptedData = try ObjectivePGP.encrypt(plainData, addSignature: false, using: keyring.keys, passphraseForKey: nil) let encryptedData = try ObjectivePGP.encrypt(plainData, addSignature: false, using: keyring.keys, passphraseForKey: nil)