https://github.com/ahmedalmasri/errorhandler
Flexible error handling for Swift and RxSwift
https://github.com/ahmedalmasri/errorhandler
Last synced: 2 months ago
JSON representation
Flexible error handling for Swift and RxSwift
- Host: GitHub
- URL: https://github.com/ahmedalmasri/errorhandler
- Owner: ahmedAlmasri
- License: mit
- Created: 2020-09-02T15:33:21.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-27T15:17:05.000Z (about 3 years ago)
- Last Synced: 2025-02-17T22:46:15.319Z (3 months ago)
- Language: Swift
- Homepage:
- Size: 379 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ErrorHandler
[](https://travis-ci.org/ahmedAlmasri/ErrorHandler)
[](https://cocoapods.org/pods/SNErrorHandler)
[](https://cocoapods.org/pods/SNErrorHandler)
[](https://cocoapods.org/pods/SNErrorHandler)## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Using
Create `ErrorMapper`
```Swift
enum MyError: Error {
case password
case unknown
}class MyErrorMapper: ErrorMappable {
func map(with error: Error) -> MyError {
if error == AfError.badRequest {
return MyError.password
}
return MyError.unknown
}}
```Create `ErrorTracker`
```Swift
let errorTracker = ErrorTracker(errorMapper: TestErrorMapper())
//For RxSwift
let rxErrorTracker = RxErrorTracker(errorMapper: TestErrorMapper())
````Create `ErrorHandler` listener
```Swift
self.errorHander = ErrorHander.catch(value: MyError.password) { _ in
errorLabel.text = "invalid password"
}.catch { error in
print("Oops!, unknown error")
}````
Throw errors
```Swift
do {
try request(.login, params: ["password": "testpass", "username": "testuser"])
} catch {
self.errorHander.throw(error)
}// For RxSwift
func binError() -> Binder {
Binder.init(self) { (_, error) inself.errorHander.throw(error)
}
}
```## Requirements
* Swift 4.0+
* Xcode 10.0+
* iOS 11.0+## Installation
ErrorHandler is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'SNErrorHandler'
```
For RxSwift
```ruby
pod 'SNErrorHandler/Rx'
```### Swift Package Manager
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
To integrate `SNErrorHandler` into your Xcode project using Xcode 11 or newer, specify it in `File > Swift Packages > Add`:
```
https://github.com/ahmedAlmasri/ErrorHandler.git
```## Author
Ahmad Almasri, [email protected]
## License
ErrorHandler is available under the MIT license. See the LICENSE file for more info.