ErrorDispatching 0.2.1

ErrorDispatching 0.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2018
SwiftSwift Version 4.0
SPMSupports SPM

Maintained by Anatoliy Radchenko, Sergey Korolkov.



  • By
  • Anatoliy Radchenko





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.