PromiseKit icon indicating copy to clipboard operation
PromiseKit copied to clipboard

Allow "filtered catch" to handle specific error types

Open ldiqual opened this issue 9 years ago • 9 comments

As described in the following Gitter conversation: https://gitter.im/mxcl/PromiseKit?at=58cf310a2215a08f04a3f9c2

I would like to have a catch per error type that I expect my promise to throw. Something similar to the way Bluebird (a popular JS Promise library) does it: http://bluebirdjs.com/docs/api/catch.html#filtered-catch

getAccount()
.catch(AccountNotFoundError) { error in
    // Only AccountNotFoundError
}
.catch { error in
    // All other errors
}

I don't think it's implementable for API users in an extension because there's a need to stop the promise chain, which I don't think can be done without a throw with the current codebase.

ldiqual avatar Mar 20 '17 18:03 ldiqual

I think the basic idea here would be to add a new catch method to the State which takes an NSError. Alternatively an Error & Compareable would be possible:

final func `catch`(on q: DispatchQueue, policy: CatchPolicy, `catch`: NSError, else resolve: @escaping (Resolution<T>) -> Void, execute body: @escaping (Error) throws -> Void) {
    pipe { resolution in
        switch (resolution, policy) {
        case(let .rejected(error, token), _) where (error as NSError) == `catch`:
            contain_zalgo(q, rejecter: resolve) {
                token.consumed = true
                try body(error)
            }
        default:
            resolve(resolution)
        }
    }
}

Inside the regular catch it is needed to have a check if the error is already consumed. If so do not execute the body.

case (let .rejected(error, token), _):
    contain_zalgo(q, rejecter: resolve) {
        if token.consumed {
            resolve(resolution)
        } else {
            token.consumed = true
            try body(error)
        }
    }
}

Now the Promis itself need the function to catch with an NSError:

@discardableResult
public func `catch`(on q: DispatchQueue = .default, policy: CatchPolicy = .allErrorsExceptCancellation, _ `catch`: NSError, execute body: @escaping (Error) -> Void) -> Promise {
    state.catch(on: q, policy: policy, catch: `catch`, else: { _ in }, execute: body)
    return self
}

Catching then can look like this:

func testCatchWithSpecificError() {
    let ex = expectation(description: "")
    
    Promise(value: ()).then {
        throw Error.more as NSError
    }.catch(Error.dummy as NSError) { _ in
        XCTFail()
    }.catch(Error.more as NSError) { _ in
        ex.fulfill()
    }.catch { _ in
        XCTFail()
    }
    waitForExpectations(timeout: 1)
}

I am not into the whole concept in depth therefor i cant provide a fully working PR. (I am not into Objc and those Objc tests do not pass.) https://github.com/mxcl/PromiseKit/compare/master...BennX:master

be-meyer avatar Sep 16 '17 11:09 be-meyer

Just out of curiosity.

What is wrong with:

// ...
}.recover { error -> Promise<String> in
    if error is SomePositiveError {
        return Promise(value: "someString")
    }   
    throw error
}.catch { error in
    switch error {
    case is AccountNotFoundError:
        // Handle AccountNotFoundError 
    default:
        // All the other errors
    }
}

basvankuijck avatar Dec 20 '17 07:12 basvankuijck

I use the recover solution when I need this pattern. There's room for something more specific though I think.

mxcl avatar Dec 20 '17 23:12 mxcl

Closing since issues is not an effective place for a wishlist. PR welcome though. Discussion here will cause me to reopen the ticket.

mxcl avatar Feb 12 '18 21:02 mxcl

@mxcl Just curious, what would be an effective place for wishlists? PRs seem fair but they involve having to show some code, which might refrain some people from adding their wishes.

ldiqual avatar Feb 12 '18 21:02 ldiqual

There is no good place for wishes on GitHub, but clogging the issues tickets inhibits my ability to effectively manage the project, so I can't have them here, sorry. With Homebrew it got to the point where bugs were lost in the noise.

Anyway, the wish is still valid and fine, a PR is the way to enact your wish, even a partial implementation would be fine for having the PR there. And it would attract interest from others since in Open Source land code is what talks.

I have appealed to people at GitHub many times to have a separate tab for wishes. Something with voting makes sense to me.

mxcl avatar Feb 12 '18 21:02 mxcl

I've seen projects – like react-navigation – use https://canny.io/. Not too sure how effective/visible it is though. Just a thought.

ldiqual avatar Feb 12 '18 21:02 ldiqual

I'm actually ok with wishes being opened here, but I can't leave them open forever in the hope that someone will PR it.

So yeah, maybe first here and then we migrate to canny.io after discussion?

mxcl avatar Feb 12 '18 21:02 mxcl

That's completely fair, thanks a lot for your insight.

ldiqual avatar Feb 12 '18 21:02 ldiqual