Allow specifying a CatchPolicy in catchOnly(Type)

Because for this error type accepting variant of
catchOnly it makes sense to want to not catch
specific cancellable cases in the type.
This commit is contained in:
Nathan Hosselton 2018-11-26 01:58:06 +00:00 committed by Max Howell
parent bd0a551392
commit eddafa8e8e
No known key found for this signature in database
GPG Key ID: B9F11512CA1D895E
2 changed files with 32 additions and 5 deletions

View File

@ -83,14 +83,16 @@ public extension CatchMixin {
- Parameter only: The error type to be caught and handled.
- Parameter on: The queue to which the provided closure dispatches.
- Parameter execute: The handler to execute if this promise is rejected.
- Note: Since this method handles only specific errors, supplying a `CatchPolicy` is unsupported. You can instead specify e.g. your cancellable error.
- SeeAlso: [Cancellation](http://promisekit.org/docs/)
*/
func catchOnly<Error: Swift.Error>(_ only: Error.Type, on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(Error) -> Void) -> PMKCascadingFinalizer {
func catchOnly<Error: Swift.Error>(_ only: Error.Type, on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) -> Void) -> PMKCascadingFinalizer {
let finalizer = PMKCascadingFinalizer()
pipe {
switch $0 {
case .rejected(let error as Error):
guard policy == .allErrors || !error.isCancelled else {
return finalizer.pending.resolver.reject(error)
}
on.async(flags: flags) {
body(error)
finalizer.pending.resolver.fulfill(())
@ -171,11 +173,10 @@ public class PMKCascadingFinalizer {
- Parameter only: The error type to be caught and handled.
- Parameter on: The queue to which the provided closure dispatches.
- Parameter execute: The handler to execute if this promise is rejected.
- Note: Since this method handles only specific errors, supplying a `CatchPolicy` is unsupported. You can instead specify e.g. your cancellable error.
- SeeAlso: [Cancellation](http://promisekit.org/docs/)
*/
public func catchOnly<Error: Swift.Error>(_ only: Error.Type, on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, _ body: @escaping(Error) -> Void) -> PMKCascadingFinalizer {
return pending.promise.catchOnly(only, on: on, flags: flags) {
public func catchOnly<Error: Swift.Error>(_ only: Error.Type, on: DispatchQueue? = conf.Q.return, flags: DispatchWorkItemFlags? = nil, policy: CatchPolicy = conf.catchPolicy, _ body: @escaping(Error) -> Void) -> PMKCascadingFinalizer {
return pending.promise.catchOnly(only, on: on, flags: flags, policy: policy) {
body($0)
}
}

View File

@ -387,6 +387,32 @@ extension CatchableTests {
wait(for: [x], timeout: 5)
}
func testCatchOnly_Type_Cancellation_Ignore() {
let x = expectation(description: #file + #function)
Promise<Int>(error: Error.cancelled).catchOnly(Error.self) { _ in
XCTFail()
x.fulfill()
}.catch(policy: .allErrors) { _ in
x.fulfill()
}
wait(for: [x], timeout: 5)
}
func testCatchOnly_Type_Cancellation_Handle() {
let x = expectation(description: #file + #function)
Promise<Int>(error: Error.cancelled).catchOnly(Error.self, policy: .allErrors) { _ in
x.fulfill()
}.catch { _ in
XCTFail()
x.fulfill()
}
wait(for: [x], timeout: 5)
}
}
private enum Error: CancellableError {