Skip to content

Instantly share code, notes, and snippets.

@rbrovko
Created August 8, 2017 09:12
Show Gist options
  • Save rbrovko/ccde599e1aaf595d06a9aa9f5baeb2a2 to your computer and use it in GitHub Desktop.
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
/**
* 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