Last active
January 7, 2017 17:28
-
-
Save wh1pch81n/0d6756921dcf84f6cf8de4b58bb99e08 to your computer and use it in GitHub Desktop.
What is one trick you can do to avoid the do-catch pyramid of doom? you can return "try method()"
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 ErrorEnum: Error { | |
| case FooError | |
| case BarError | |
| } | |
| func bar(success: Bool) throws -> String { | |
| if success { | |
| return "bar success" | |
| } | |
| throw ErrorEnum.BarError | |
| } | |
| func foo(success: (Bool?, Bool)) throws -> String { | |
| if case(true?) = success.0 { | |
| return "foo success" | |
| } else if case(false?) = success.0 { | |
| return try bar(success: success.1) | |
| } else { | |
| throw ErrorEnum.FooError | |
| } | |
| } | |
| do { | |
| try foo(success: (true, true)) // "foo success" | |
| } catch ErrorEnum.FooError { | |
| "foo error" | |
| } catch ErrorEnum.BarError { | |
| "bar error" | |
| } catch { | |
| "unhandled error" | |
| } | |
| do { | |
| try foo(success: (nil, false)) | |
| } catch ErrorEnum.FooError { | |
| "foo error" // "foo error" | |
| } catch ErrorEnum.BarError { | |
| "bar error" | |
| } catch { | |
| "unhandled error" | |
| } | |
| do { | |
| try foo(success: (false, true)) // "bar success" | |
| } catch ErrorEnum.FooError { | |
| "foo error" | |
| } catch ErrorEnum.BarError { | |
| "bar error" | |
| } catch { | |
| "unhandled error" | |
| } | |
| do { | |
| try foo(success: (false, false)) | |
| } catch ErrorEnum.FooError { | |
| "foo error" | |
| } catch ErrorEnum.BarError { | |
| "bar error" // "bar error" | |
| } catch { | |
| "unhandled error" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment