|
9 | 9 | alt="Build Status"> |
10 | 10 | </a> |
11 | 11 | <a href="https://github.com/JamitLabs/MungoHealer/releases"> |
12 | | - <img src="https://img.shields.io/badge/Version-0.2.0-blue.svg" |
13 | | - alt="Version: 0.2.0"> |
| 12 | + <img src="https://img.shields.io/badge/Version-0.3.0-blue.svg" |
| 13 | + alt="Version: 0.3.0"> |
14 | 14 | </a> |
15 | 15 | <img src="https://img.shields.io/badge/Swift-4.2-FFAC45.svg" |
16 | 16 | alt="Swift: 4.2"> |
@@ -40,6 +40,53 @@ When developing a new feature for an App developers often need to both have pres |
40 | 40 |
|
41 | 41 | While there are many ways to deal with such situations, MungoHealer provides a straightforward and Swift-powered approach that uses system alerts for user feedback by default, but can be easily customized to use custom UI when needed. |
42 | 42 |
|
| 43 | +## tl;dr |
| 44 | + |
| 45 | +Here's a very simple example of basic error handling without MungoHealer: |
| 46 | + |
| 47 | +```swift |
| 48 | +func login(success: (String) -> Void) { |
| 49 | + guard let username = usernameLabel.text, !username.isEmpty else { |
| 50 | + let alertCtrl = UIAlertController(title: "Invalid User Input", message: "Please enter a username.", preferredStyle: .alert) |
| 51 | + alertCtrl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) |
| 52 | + viewController.present(alertCtrl, animated: true, completion: nil) |
| 53 | + return |
| 54 | + } |
| 55 | + guard let password = passwordLabel.text, !password.isEmpty else { |
| 56 | + let alertCtrl = UIAlertController(title: "Invalid User Input", message: "Please enter a password.", preferredStyle: .alert) |
| 57 | + alertCtrl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) |
| 58 | + viewController.present(alertCtrl, animated: true, completion: nil) |
| 59 | + return |
| 60 | + } |
| 61 | + guard let apiToken = getApiToken(username, password) else { |
| 62 | + let alertCtrl = UIAlertController(title: "Invalid User Input", message: "Username and password did not match.", preferredStyle: .alert) |
| 63 | + alertCtrl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) |
| 64 | + viewController.present(alertCtrl, animated: true, completion: nil) |
| 65 | + return |
| 66 | + } |
| 67 | + success(apiToken) |
| 68 | +) |
| 69 | +``` |
| 70 | + |
| 71 | +Using MungoHealer the above code becomes this: |
| 72 | + |
| 73 | +```swift |
| 74 | +func login(success: (String) -> Void) { |
| 75 | + mungo.do { |
| 76 | + guard let username = usernameLabel.text, !username.isEmpty else { |
| 77 | + throw MungoError(source: .invalidUserInput, message: "Please enter a username.") |
| 78 | + } |
| 79 | + guard let password = passwordLabel.text, !password.isEmpty else { |
| 80 | + throw MungoError(source: .invalidUserInput, message: "Please enter a password.") |
| 81 | + } |
| 82 | + guard let apiToken = getApiToken(username, password) else { |
| 83 | + throw MungoError(source: .invalidUserInput, message: "Username and password did not match.") |
| 84 | + } |
| 85 | + success(apiToken) |
| 86 | + } |
| 87 | +) |
| 88 | +``` |
| 89 | + |
43 | 90 | ## Installation |
44 | 91 |
|
45 | 92 | Installing via [Carthage](https://github.com/Carthage/Carthage#carthage) & [CocoaPods](https://guides.cocoapods.org/using/getting-started.html) are both supported. |
@@ -340,6 +387,16 @@ private func loadAvatarImage() { |
340 | 387 |
|
341 | 388 | We don't need to deal with error handling on the call side which makes our code both more readable & more fun to write. Instead, we define how to deal with the errors at the point where the error is thrown/defined. On top of that, the way errors are communicated to the user is abstracted away and can be changed App-wide by simply editing the error handler code. This also makes it possible to handle errors in the model or networking layer without referencing any `UIKit` classes. |
342 | 389 |
|
| 390 | +For cases where you just want one catch-all where you just call the `handle(error)` method, there's even a shorthand which will deal with this automatically. Just use this instead of the above code: |
| 391 | + |
| 392 | +```swift |
| 393 | +private func loadAvatarImage() { |
| 394 | + mungo.do { |
| 395 | + imageView.image = try fetchImage(urlPath: user.avatarUrlPath) |
| 396 | + } |
| 397 | +} |
| 398 | +``` |
| 399 | + |
343 | 400 | So as you can see, used wisely, MungoHealer can help to make your code **cleaner**, **less error prone** and it can **improve the User Experience** for your users. |
344 | 401 |
|
345 | 402 | ## Contributing |
|
0 commit comments