Skip to content

Instantly share code, notes, and snippets.

@drumnkyle
Created June 15, 2015 05:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save drumnkyle/e81becfb3e207027bf07 to your computer and use it in GitHub Desktop.
Swift 2 Error Handling with asynchronous closures
// Error Handling //
enum Either<T> {
case Success(T)
case Failure(ErrorType)
func getValue() throws -> T {
switch self {
case .Success(let value):
return value
case .Failure(let error):
throw error
}
}
static func run(closure: () throws -> T) -> Either<T> {
do {
let result = try closure()
return .Success(result)
} catch {
return .Failure(error)
}
}
}
let r = Either.run {
return 12
}
do {
let value = try r.getValue()
} catch {
print(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment