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: %@";
"FailedToSavePasswordEntity" = "Failed to save password entity: %@";
"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.";
"PasswordDuplicatedError." = "Cannot add the password; password is duplicated.";
"GitResetError." = "Cannot identify the latest synced commit.";
"PGPPublicKeyNotExistError." = "PGP public key doesn't exist.";
"WrongPasswordFilename." = "Cannot write to the password file.";
"PgpPublicKeyNotExistError." = "PGP public key doesn't exist.";
"WrongPasswordFilenameError." = "Cannot write to the password file.";
"DecryptionError." = "Cannot decrypt password.";
"UnknownError." = "Unknown error.";
"PrepareRepository" = "Prepare Repository";

View file

@ -6,23 +6,27 @@
// Copyright © 2017 Bob Sun. All rights reserved.
//
import Foundation
public enum AppError: Error {
case RepositoryNotSetError
case RepositoryRemoteBranchNotFoundError(_: String)
case RepositoryNotSet
case RepositoryRemoteBranchNotFound(_: String)
case RepositoryBranchNotFound(_: String)
case KeyImportError
case PasswordDuplicatedError
case GitResetError
case PGPPublicKeyNotExistError
case KeyImport
case PasswordDuplicated
case GitReset
case PgpPublicKeyNotExist
case WrongPasswordFilename
case DecryptionError
case UnknownError
case Decryption
case Unknown
}
extension AppError: LocalizedError {
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
self.publicKey = importKey(from: keyPath)
if self.publicKey == nil {
throw AppError.KeyImportError
throw AppError.KeyImport
}
case .secret:
let keyPath = Globals.pgpPrivateKeyPath
self.privateKey = importKey(from: keyPath)
if self.privateKey == nil {
throw AppError.KeyImportError
throw AppError.KeyImport
}
default:
throw AppError.UnknownError
throw AppError.Unknown
}
}
@ -335,14 +335,14 @@ public class PasswordStore {
return
}
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
let remoteBranchName = "origin/\(localBranchName)"
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 {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName)
throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
}
let localBranch = try storeRepository.createBranchNamed(localBranchName, from: remoteBranchOid, message: nil)
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 {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
let options = [GTRepositoryRemoteOptionsCredentialProvider: credentialProvider]
@ -519,7 +519,7 @@ public class PasswordStore {
private func gitAdd(path: String) throws {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
try storeRepository.index().addFile(path)
try storeRepository.index().write()
@ -527,7 +527,7 @@ public class PasswordStore {
private func gitRm(path: String) throws {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
let url = storeURL.appendingPathComponent(path)
if fm.fileExists(atPath: url.path) {
@ -562,7 +562,7 @@ public class PasswordStore {
private func gitCommit(message: String) throws -> GTCommit? {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
let newTree = try storeRepository.index().writeTree()
let headReference = try storeRepository.headReference()
@ -576,7 +576,7 @@ public class PasswordStore {
private func getLocalBranch(withName branchName: String) throws -> GTBranch? {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
let reference = GTBranch.localNamePrefix().appending(branchName)
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 {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
do {
let credentialProvider = try credential.credentialProvider(requestGitPassword: requestGitPassword)
@ -601,7 +601,7 @@ public class PasswordStore {
private func addPasswordEntities(password: Password) throws -> PasswordEntity? {
guard !passwordExisted(password: password) else {
throw AppError.PasswordDuplicatedError
throw AppError.PasswordDuplicated
}
var passwordURL = password.url
@ -784,7 +784,7 @@ public class PasswordStore {
// return the number of discarded commits
public func reset() throws -> Int {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
// get a list of local commits
if let localCommits = try getLocalCommits(),
@ -793,7 +793,7 @@ public class PasswordStore {
guard let firstLocalCommit = localCommits.last,
firstLocalCommit.parents.count == 1,
let newHead = firstLocalCommit.parents.first else {
throw AppError.GitResetError
throw AppError.GitReset
}
try storeRepository.reset(to: newHead, resetType: .hard)
self.setAllSynced()
@ -810,16 +810,16 @@ public class PasswordStore {
private func getLocalCommits() throws -> [GTCommit]? {
guard let storeRepository = storeRepository else {
throw AppError.RepositoryNotSetError
throw AppError.RepositoryNotSet
}
// get the remote branch
let remoteBranchName = SharedDefaults[.gitBranchName]!
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
guard remoteBranch.oid != nil else {
throw AppError.RepositoryRemoteBranchNotFoundError(remoteBranchName)
throw AppError.RepositoryRemoteBranchNotFound(remoteBranchName)
}
// 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 plainText = String(data: decryptedData, encoding: .utf8) ?? ""
guard let url = passwordEntity.getURL() else {
throw AppError.DecryptionError
throw AppError.Decryption
}
return Password(name: passwordEntity.getName(), url: url, plainText: plainText)
}
public func encrypt(password: Password) throws -> Data {
guard keyring.keys.count > 0 else {
throw AppError.PGPPublicKeyNotExistError
throw AppError.PgpPublicKeyNotExist
}
let plainData = password.plainData
let encryptedData = try ObjectivePGP.encrypt(plainData, addSignature: false, using: keyring.keys, passphraseForKey: nil)