Created
August 8, 2017 09:12
-
-
Save rbrovko/ccde599e1aaf595d06a9aa9f5baeb2a2 to your computer and use it in GitHub Desktop.
A function that enables you to easily wrap throwing APIs, to provide a custom error
This file contains 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
/** | |
* Perform a throwing expression, and throw a custom error in case the expression threw | |
* | |
* - parameter expression: The expression to execute | |
* - parameter error: The custom error to throw instead of the expression's error | |
* - throws: The given error | |
* - returns: The return value of the given expression | |
*/ | |
func perform<T>(_ expression: @autoclosure () throws -> T, orThrow error: Error) throws -> T { | |
do { | |
return try expression() | |
} catch { | |
throw error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment