You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ErrorHandler/Classes/Core/Matchers.swift
+28-2Lines changed: 28 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -8,24 +8,50 @@
8
8
9
9
import Foundation
10
10
11
+
/**
12
+
An `ErrorMatcher` is an alternative to using `matches` closures when adding `ErrorActions` to an `ErrorHandler`. An `ErrorMatcher` is considered to match an error when it's `matches` function returns `true` for this error. `ErrorMatcher`s are used to match errors with the `ErrorHandler`'s `on(_ matcher: ErrorMatcher, do action: @escaping ErrorAction)` method.
13
+
14
+
`ErrorMatcher`s can be combined using || and && operators.
15
+
For example:
16
+
17
+
```
18
+
let notConnectedMatcher = NSErrorMatcher(domain: NSURLErrorDomain, code: NSURLErrorNotConnectedToInternet)
19
+
20
+
let connectionLostMatcher = NSErrorMatcher(domain: NSURLErrorDomain, code: NSURLErrorNetworkConnectionLost)
21
+
22
+
let offlineMatcher = notConnectedMatcher || connectionLostMatcher
23
+
```
24
+
Error matchers have the additional benefit compared to `matches` closures that they describe matching logic in a way that can be more naturally reused. i.e. it is more natural in `Swift` to reuse Types than free functions and closures as the unit of composition in Swift is the type.
25
+
*/
11
26
publicprotocolErrorMatcher{
27
+
28
+
/**
29
+
The `ErrorMatcher` is considered to match the error if this function returns true.
30
+
- Returns: `true` if the matcher matches the `error` otherwise `false`
31
+
*/
12
32
func matches(_ error:Error)->Bool
13
33
}
14
34
35
+
/**
36
+
A generic `ErrorMatcher` over type `E` that `matches` an error if the error `is` `T`
0 commit comments