Use LocalizedError and not Error in swift, and override errorDescription

This commit is contained in:
Thom Chiovoloni 2019-06-25 10:29:31 -07:00
Родитель 97b99e38b8
Коммит 80e4ffc11d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 31F01AEBD799934A
3 изменённых файлов: 73 добавлений и 3 удалений

Просмотреть файл

@ -7,12 +7,26 @@ import Foundation
// FIXME: these should be lower case.
// swiftlint:disable identifier_name
public enum FirefoxAccountError: Error {
public enum FirefoxAccountError: LocalizedError {
case Unauthorized(message: String)
case Network(message: String)
case Unspecified(message: String)
case Panic(message: String)
/// Our implementation of the localizedError protocol -- (This shows up in Sentry)
public var errorDescription: String? {
switch self {
case let .Unauthorized(message):
return "FirefoxAccountError.Unauthorized: \(message)"
case let .Network(message):
return "FirefoxAccountError.Network: \(message)"
case let .Unspecified(message):
return "FirefoxAccountError.Unspecified: \(message)"
case let .Panic(message):
return "FirefoxAccountError.Panic: \(message)"
}
}
// The name is attempting to indicate that we free fxaError.message if it
// existed, and that it's a very bad idea to touch it after you call this
// function

Просмотреть файл

@ -5,7 +5,7 @@
import Foundation
/// Indicates an error occurred while calling into the logins storage layer
public enum LoginsStoreError: Error {
public enum LoginsStoreError: LocalizedError {
/// This is a catch-all error code used for errors not yet exposed to consumers,
/// typically since it doesn't seem like there's a sane way for them to be handled.
case unspecified(message: String)
@ -45,6 +45,30 @@ public enum LoginsStoreError: Error {
/// abort some operation.
case interrupted(message: String)
/// Our implementation of the localizedError protocol -- (This shows up in Sentry)
public var errorDescription: String? {
switch self {
case let .unspecified(message):
return "LoginsStoreError.unspecified: \(message)"
case let .panic(message):
return "LoginsStoreError.panic: \(message)"
case let .authInvalid(message):
return "LoginsStoreError.authInvalid: \(message)"
case let .noSuchRecord(message):
return "LoginsStoreError.noSuchRecord: \(message)"
case let .duplicateGuid(message):
return "LoginsStoreError.duplicateGuid: \(message)"
case let .invalidLogin(message):
return "LoginsStoreError.invalidLogin: \(message)"
case let .invalidKey(message):
return "LoginsStoreError.invalidKey: \(message)"
case let .network(message):
return "LoginsStoreError.network: \(message)"
case let .interrupted(message):
return "LoginsStoreError.interrupted: \(message)"
}
}
// The name is attempting to indicate that we free rustError.message if it
// existed, and that it's a very bad idea to touch it after you call this
// function

Просмотреть файл

@ -6,7 +6,7 @@ import Foundation
import os.log
/// Indicates an error occurred while calling into the places storage layer
public enum PlacesError: Error {
public enum PlacesError: LocalizedError {
/// This indicates an attempt to use a connection after the PlacesAPI
/// it came from is destroyed. This indicates a usage error of this library.
case connUseAfterAPIClosed
@ -56,6 +56,38 @@ public enum PlacesError: Error {
/// insert a new item as a child of root________.
case cannotUpdateRoot(message: String)
/// Our implementation of the localizedError protocol -- (This shows up in Sentry)
public var errorDescription: String? {
switch self {
case .connUseAfterAPIClosed:
return "PlacesError.connUseAfterAPIClosed"
case let .unexpected(message):
return "PlacesError.unexpected: \(message)"
case let .panic(message):
return "PlacesError.panic: \(message)"
case let .invalidPlace(message):
return "PlacesError.invalidPlace: \(message)"
case let .urlParseError(message):
return "PlacesError.urlParseError: \(message)"
case let .databaseBusy(message):
return "PlacesError.databaseBusy: \(message)"
case let .databaseInterrupted(message):
return "PlacesError.databaseInterrupted: \(message)"
case let .databaseCorrupt(message):
return "PlacesError.databaseCorrupt: \(message)"
case let .invalidParent(message):
return "PlacesError.invalidParent: \(message)"
case let .noSuchItem(message):
return "PlacesError.noSuchItem: \(message)"
case let .urlTooLong(message):
return "PlacesError.urlTooLong: \(message)"
case let .illegalChange(message):
return "PlacesError.illegalChange: \(message)"
case let .cannotUpdateRoot(message):
return "PlacesError.cannotUpdateRoot: \(message)"
}
}
// The name is attempting to indicate that we free rustError.message if it
// existed, and that it's a very bad idea to touch it after you call this
// function