Skip to content

Instantly share code, notes, and snippets.

@OneSadCookie
Created November 14, 2015 03:15
Show Gist options
  • Save OneSadCookie/4d63b78d67df78299699 to your computer and use it in GitHub Desktop.
Save OneSadCookie/4d63b78d67df78299699 to your computer and use it in GitHub Desktop.
enum TheirError: ErrorType {
case Something
}
enum MyError: ErrorType {
case CouldNot(TheirError)
}
func Foo() throws -> String {
throw TheirError.Something
//return "x"
}
// Meh
// ------------
let s: String
do {
s = try Foo()
} catch let err as TheirError {
throw MyError.CouldNot(err)
}
// Experiment (which I surely won't use because it's still too weird ;-)
// ------------
func errorWrap<Result, OriginalError: ErrorType, WrappingError: ErrorType>(@autoclosure closure: () throws -> Result, _ wrapError: OriginalError -> WrappingError) throws -> Result {
do {
return try closure()
} catch let x as OriginalError {
throw wrapError(x)
}
}
let x:String = try errorWrap(Foo(), MyError.CouldNot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment