Created
March 30, 2015 11:19
-
-
Save brocoo/37fd428e5a187426c3ab to your computer and use it in GitHub Desktop.
Extension of NSError that allow custom personalised errors for a project (Swift 1.2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - NSError | |
public enum ErrorType: Int { | |
case Unknown = 1 | |
case NotAuthenticated = 2 | |
func localizedUserInfo() -> [String: String] { | |
var localizedDescription: String = "" | |
var localizedFailureReasonError: String = "" | |
var localizedRecoverySuggestionError: String = "" | |
switch self { | |
case Unknown: | |
localizedDescription = NSLocalizedString("Error.Unknown", comment: "Unknown error") | |
case NotAuthenticated: | |
localizedDescription = NSLocalizedString("Error.NotAuthenticated", comment: "User not authenticated") | |
} | |
return [ | |
NSLocalizedDescriptionKey: localizedDescription, | |
NSLocalizedFailureReasonErrorKey: localizedFailureReasonError, | |
NSLocalizedRecoverySuggestionErrorKey: localizedRecoverySuggestionError | |
] | |
} | |
} | |
public let ProjectErrorDomain = "ProjectErrorDomain" | |
extension NSError { | |
public convenience init(type: ErrorType) { | |
self.init(domain: ProjectErrorDomain, code: type.rawValue, userInfo: type.localizedUserInfo()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment