Error handling in Objective-C is mostly done through
- For synchronous functions, we pass the double pointer of
NSErrorto a function that can fail and if the function hits a error condition, it will set the error object and return
- For asynchronous functions,
NSErroris passed as an argument to the completion block
We can do follow the same Pattern in Swift, but it is not advised. There are more swiftier ways of handling errors and in this post I’ll be discussing them.
Optional is a box that can contain a value or nothing. This is particularly suited when a function can fail, but we are not concerned about why it failed.
In some cases, ignoring errors might not be an option. We might need to do different things based on why a particular method failed. Using
try-catch is best option in this case.
Lately, I have seen lot of open source projects using this method to handle errors in more functional manner.
This is an extension of
Optional pattern where in error information is not discarded.
An Optional, in swift, is implemented as:
From the above definition, we can see that,
.Some can carry set value, but
.None has no associated data. Let’s define another enum that has associated error type for
.None case as well:
Now, failing functions can be made to return this
Even though above version might look like it has a lot of code, I really like the fact that every function does what it’s supposed to do. Nothing more, nothing less.
This Result enum is already packed into a neat little library by Antitypical with all the bells and whistles.
I’m still digging into this pattern and will hopely write more about it in future…