Skip to content

Instantly share code, notes, and snippets.

@jschmid
Created September 2, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschmid/25e91ea9c02f70b7287f to your computer and use it in GitHub Desktop.
Save jschmid/25e91ea9c02f70b7287f to your computer and use it in GitHub Desktop.
Specialized errors
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