Skip to content

Commit ecf6950

Browse files
committed
Provide a way out in case Swift compiler really doesn't know which
method or property is meant by `error`
1 parent 8db942b commit ecf6950

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

Sources/Promise+Properties.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ extension Promise {
1313
}
1414
}
1515

16+
/**
17+
- Returns: The error with which this promise was rejected; `nil` if this promise is not rejected.
18+
Alternative for the cases where the Swift compiler gets confused over the name of the `error` property
19+
*/
20+
public var errorValue: ErrorType? {
21+
return self.error
22+
}
23+
1624
/**
1725
- Returns: `true` if the promise has not yet resolved.
1826
*/

Sources/Promise.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ public class Promise<T> {
390390
}
391391
}
392392

393+
/**
394+
Provides an alias for the `error` function for cases where the Swift compiler gets into trouble inferring the right
395+
types. If `error` does not work, consider using `onError` instead which does the same thing.
396+
*/
397+
public func onError(policy policy: ErrorPolicy = .AllErrorsExceptCancellation, _ body: (ErrorType) -> Void) {
398+
error(policy: policy, body)
399+
}
400+
393401
/**
394402
The provided closure is executed when this promise is rejected giving you
395403
an opportunity to recover from the error and continue the promise chain.

Tests/CorePromise/ErrorUnhandler.test.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ class ErrorHandlingTests_Swift: XCTestCase {
116116
}
117117
}
118118

119+
120+
// an alias called `onError` exists for the `error` function
121+
func test7() {
122+
twice { promise, ex in
123+
PMKUnhandledErrorHandler = { err in
124+
XCTFail()
125+
}
126+
127+
promise.onError { error in
128+
ex.fulfill()
129+
}
130+
}
131+
}
132+
119133
func testDoubleRejectDoesNotTriggerUnhandler() {
120134
enum Error: ErrorType {
121135
case Test

0 commit comments

Comments
 (0)