Latest 0.2.0
Homepage https://github.com/eastsss/ErrorDispatching
License MIT
Platforms ios 8.0
Authors

CI Status
Version
Swift v4
License
Platform

What is this?

A small library which simplifies error handling code in your view controllers/view models by using Chain of Responsibility pattern. It also allows you to reuse error handling code if you need the same behavior in another project.

How it works?

One of the main things here are so called "proposers". Every proposer usually takes one specific type of error as an input and proposes method to handle this error – for example, "Show a system alert", or passes this error down by hierarchy. Executing this method(e.g. showing a system alert) is completely up to you.

Handling your own custom errors is pretty simple – create a class which implements MethodProposing protocol and return correct "proposition" for your errors:

enum MyCustomError: Swift.Error {
    case somethingBadHappened
}

class MyProposer: MethodProposing {
    func proposeMethod(toHandle error: Error) -> Proposition? {
        guard let myCustomError = error as? MyCustomError else {
            return nil
        }

        let config = SystemAlertConfiguration(
            title: "Error",
            message: "Oops! Something went wrong",
            actionTitle: "OK"
        )

        return .single(.systemAlert(config))
    }
}

Then you can pass this proposer to ErrorDispatcher initializator, as well as any other proposers you need:

let compoundProposer = CompoundMethodProposer(proposers: [
    MyProposer(),
    NSURLErrorMethodProposer()
])
let dispatcher: ErrorDispatcher = ErrorDispatcher(proposer: compoundProposer)

To execute proposed methods, you should implement MethodExecutor protocol. This is a callback which will be called when dispatcher found a "method" to handle this error. Example:

extension ExampleViewController: MethodExecutor {
    func execute(method: ErrorHandlingMethod) {
        switch method {
        case .systemAlert(let config):
            showSystemAlert(with: config)
        default:
            return
        }
    }
}

After that you can use ErrorDispatcher to handle your errors as following:

dispatcher.executor = self

let nsError = NSError(domain: NSURLErrorDomain, code: NSURLErrorCannotFindHost, userInfo: nil)
dispatcher.handle(error: nsError)

Example

Just launch Example/ErrorDispatching.xcworkspace, build and run. Cocoapods are already installed.

Installation

ErrorDispatching is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "ErrorDispatching"

Versions

Swift Version Pod Version
3.0 0.1.3+
4.0 0.2.0+

TODO

  • Add more built-in proposers for system errors
  • More localizations as well

Author

Anatoliy Radchenko, [email protected]

License

ErrorDispatching is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "ErrorDispatching",
    "version": "0.2.0",
    "summary": "A simple and reusable error handling in Swift",
    "homepage": "https://github.com/eastsss/ErrorDispatching",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Anatoliy Radchenko": "[email protected]"
    },
    "source": {
        "git": "https://github.com/eastsss/ErrorDispatching.git",
        "tag": "0.2.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "default_subspecs": "Core",
    "pushed_with_swift_version": "4.0",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "ErrorDispatching/Classes/Core/**/*",
            "frameworks": [
                "Foundation",
                "UIKit"
            ],
            "resource_bundles": {
                "ErrorDispatching": [
                    "ErrorDispatching/Assets/Core/**/*.{strings}"
                ]
            }
        },
        {
            "name": "ReactiveSwift",
            "source_files": "ErrorDispatching/Classes/ReactiveSwift/**/*",
            "dependencies": {
                "ErrorDispatching/Core": [],
                "ReactiveSwift": [
                    "~> 2.0"
                ],
                "Result": [
                    "~> 3.0"
                ]
            }
        },
        {
            "name": "Moya",
            "source_files": "ErrorDispatching/Classes/Moya/**/*",
            "dependencies": {
                "ErrorDispatching/Core": [],
                "Moya": [
                    "~> 9.0"
                ]
            }
        }
    ]
}

Pin It on Pinterest

Share This