Created
November 14, 2015 03:15
-
-
Save OneSadCookie/4d63b78d67df78299699 to your computer and use it in GitHub Desktop.
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
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