Created
September 2, 2015 08:19
-
-
Save jschmid/25e91ea9c02f70b7287f to your computer and use it in GitHub Desktop.
Specialized errors
This file contains hidden or 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
import UIKit | |
// Specialized error type | |
public protocol CommandError: ErrorType {} | |
// A command that does not have specialized errors | |
public enum NoCommandError: CommandError {} | |
// The "Authorize" method returns errors | |
public enum AuthorizeError: CommandError { | |
case WrongAuthorizeCode | |
case WrongRequestId | |
} | |
// The errors the client can return | |
public enum ClientError<T: CommandError>: ErrorType { | |
case Deserialization | |
case Networking(internalError: NSError) | |
case CommandError(commandError: T) | |
case Unknown | |
} | |
// For any method that does not return specialized errors | |
public typealias ClientErrorDefault = ClientError<NoCommandError> | |
func testAuthorize() -> ClientError<AuthorizeError> { | |
return ClientError.CommandError(commandError: AuthorizeError.WrongAuthorizeCode) | |
} | |
func testOther() -> ClientErrorDefault { | |
return ClientError.Deserialization | |
} | |
func matchError() { | |
let error = testAuthorize() | |
// let error = testOther() | |
switch error { | |
case .Deserialization: print("Deserialization") | |
case .Networking(let e): print("Networking: \(e)") | |
case .Unknown: print("Unknown") | |
case .CommandError(let e): print(e) | |
} | |
} | |
matchError() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment