Skip to content

Commit 1caef0c

Browse files
author
elenipapanik
committed
Add documentation to Matchers
1 parent c4af258 commit 1caef0c

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

ErrorHandler/Classes/Core/Matchers.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,50 @@
88

99
import Foundation
1010

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+
*/
1126
public protocol ErrorMatcher {
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+
*/
1232
func matches(_ error: Error) -> Bool
1333
}
1434

35+
/**
36+
A generic `ErrorMatcher` over type `E` that `matches` an error if the error `is` `T`
37+
*/
1538
public class ErrorTypeMatcher<E: Error>: ErrorMatcher {
1639
public init() {}
1740
public func matches(_ error: Error) -> Bool {
1841
return error is E
1942
}
2043
}
2144

45+
/**
46+
An `ErrorMatcher` that wraps a `matches` closure
47+
*/
2248
public class ClosureErrorMatcher: ErrorMatcher {
2349
private let matches: (Error) -> Bool
24-
50+
2551
public init(matches: @escaping (Error) -> Bool) {
2652
self.matches = matches
2753
}
28-
54+
2955
public func matches(_ error: Error) -> Bool {
3056
return self.matches(error)
3157
}

0 commit comments

Comments
 (0)