Skip to content

Commit

Permalink
Merge branch 'deploy/0.3.0' into productive
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Oct 24, 2018
2 parents 0a0732f + 89874ee commit ccef9e4
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .projlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rules:
SwiftLint: |
if [ "${CONFIGURATION}" = "Debug" ]; then
if which swiftlint > /dev/null; then
swiftlint
swiftlint --quiet
else
echo "warning: SwiftLint not installed, download it from https://github.com/realm/SwiftLint"
fi
Expand Down
4 changes: 2 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ custom_rules:
severity: warning
controller_class_name_suffix:
included: ".*.swift"
regex: 'class +\w+(?<!View|Flow)Controller'
regex: 'class +\w+(?<!View|Flow|Model|Presentation)Controller'
name: "Controller Class Name Suffix"
message: "Only use the `Controller` class name suffix for ViewControllers or FlowControllers."
message: "Only use the `Controller` class name suffix for View-, Flow-, Model- and PresentationControllers."
severity: warning
debug_log_level:
included: ".*.swift"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

## [0.3.0] - 2018-10-24
### Added
- Convenience `do` method on `MungoHealer` to simplify usage with single `handle(error)` catch-all.

## [0.2.0] - 2018-10-24
### Added
- Convenience implementations of error protocols: `MungoError`, `MungoFatalError` & `MungoHealableError`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,32 @@ struct MyHealableError: HealableError {

class ViewController: UIViewController {
@IBAction func throwErrorButtonPressed() {
do {
mungo.do {
throw MyError()
} catch {
mungo.handle(error)
}
}

@IBAction func throwLocalizedErrorButtonPressed() {
do {
mungo.do {
throw MyLocalizedError()
} catch {
mungo.handle(error)
}
}

@IBAction func throwBaseErrorButtonPressed() {
do {
mungo.do {
throw MyBaseError()
} catch {
mungo.handle(error)
}
}

@IBAction func throwFatalErrorButtonPressed() {
do {
mungo.do {
throw MyFatalError()
} catch {
mungo.handle(error)
}
}

@IBAction func throwHealableErrorButtonPressed() {
do {
mungo.do {
throw MyHealableError(retryClosure: { [weak self] in self?.throwHealableErrorButtonPressed() })
} catch {
mungo.handle(error)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions Frameworks/MungoHealer/MungoHealer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ public class MungoHealer {
errorHandler.handle(error: error)
}
}

/// Shorthand syntax for handling all errors using `mungo.handle(error)`.
///
/// - Parameter closure: The throwing code to be handled automatically if needed.
public func `do`(_ closure: () throws -> Void) {
do {
try closure()
} catch {
handle(error)
}
}
}
2 changes: 1 addition & 1 deletion Frameworks/SupportingFiles/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.2.0</string>
<string>0.3.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 1 addition & 1 deletion MungoHealer.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "MungoHealer"
s.version = "0.2.0"
s.version = "0.3.0"
s.summary = "Error Handler based on localized & healable (recoverable) errors without the overhead of NSError. "

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion MungoHealer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if [ \"${CONFIGURATION}\" = \"Debug\" ]; then\n if which swiftlint > /dev/null; then\n swiftlint\n else\n echo \"warning: SwiftLint not installed, download it from https://github.com/realm/SwiftLint\"\n fi\nfi\n";
shellScript = "if [ \"${CONFIGURATION}\" = \"Debug\" ]; then\n if which swiftlint > /dev/null; then\n swiftlint --quiet\n else\n echo \"warning: SwiftLint not installed, download it from https://github.com/realm/SwiftLint\"\n fi\nfi\n";
};
A102BDD81F023D6C000C6B38 /* SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
Expand Down
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
alt="Build Status">
</a>
<a href="https://github.com/JamitLabs/MungoHealer/releases">
<img src="https://img.shields.io/badge/Version-0.2.0-blue.svg"
alt="Version: 0.2.0">
<img src="https://img.shields.io/badge/Version-0.3.0-blue.svg"
alt="Version: 0.3.0">
</a>
<img src="https://img.shields.io/badge/Swift-4.2-FFAC45.svg"
alt="Swift: 4.2">
Expand Down Expand Up @@ -40,6 +40,53 @@ When developing a new feature for an App developers often need to both have pres

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.

## tl;dr

Here's a very simple example of basic error handling without MungoHealer:

```swift
func login(success: (String) -> Void) {
guard let username = usernameLabel.text, !username.isEmpty else {
let alertCtrl = UIAlertController(title: "Invalid User Input", message: "Please enter a username.", preferredStyle: .alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
viewController.present(alertCtrl, animated: true, completion: nil)
return
}
guard let password = passwordLabel.text, !password.isEmpty else {
let alertCtrl = UIAlertController(title: "Invalid User Input", message: "Please enter a password.", preferredStyle: .alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
viewController.present(alertCtrl, animated: true, completion: nil)
return
}
guard let apiToken = getApiToken(username, password) else {
let alertCtrl = UIAlertController(title: "Invalid User Input", message: "Username and password did not match.", preferredStyle: .alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
viewController.present(alertCtrl, animated: true, completion: nil)
return
}
success(apiToken)
)
```

Using MungoHealer the above code becomes this:

```swift
func login(success: (String) -> Void) {
mungo.do {
guard let username = usernameLabel.text, !username.isEmpty else {
throw MungoError(source: .invalidUserInput, message: "Please enter a username.")
}
guard let password = passwordLabel.text, !password.isEmpty else {
throw MungoError(source: .invalidUserInput, message: "Please enter a password.")
}
guard let apiToken = getApiToken(username, password) else {
throw MungoError(source: .invalidUserInput, message: "Username and password did not match.")
}
success(apiToken)
}
)
```

## Installation

Installing via [Carthage](https://github.com/Carthage/Carthage#carthage) & [CocoaPods](https://guides.cocoapods.org/using/getting-started.html) are both supported.
Expand Down Expand Up @@ -340,6 +387,16 @@ private func loadAvatarImage() {

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.

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:

```swift
private func loadAvatarImage() {
mungo.do {
imageView.image = try fetchImage(urlPath: user.avatarUrlPath)
}
}
```

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.

## Contributing
Expand Down

0 comments on commit ccef9e4

Please sign in to comment.