google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.8k stars 293 forks source link

Type Checking error in catch block does not work as expected #81

Closed technocidal closed 5 years ago

technocidal commented 5 years ago

Suppose one has a scenario where a promise/chain of promises can be potentially rejected with several different error types.

When switching over the error

switch error {
case is PromiseError:
    break
default:
    break
}

or casting directly

switch error {
case let error as PromiseError:
    break
default:
    break
}

will never succeed.

This makes it really hard to test for PromiseError.timedOut, when other errors need to be handled as well.

The work around is quite unintuitive as one first needs to cast the error to NSError and then check the error domain.

switch error {
case let error as NSError:
    if error.domain == PromiseError.errorDomain {

    }
default:
    break
}

If one wants to specifically target the PromiseError.timedOut one would need to check for the PromiseError.timedOut.errorCode as well.

Am I doing something wrong or is there a specific reason for this?

shoumikhin commented 5 years ago

Hi Johannes,

Thanks a lot for reporting this, looks like we forgot to convert the underlying NSError to PromiseError.timedOut case for the timeout operator in Swift.

If you like, feel free to submit a PR with the fix, or use the following workaround to catch .timedOut properly:

switch PromiseError(error) ?? error {
case is PromiseError:
    break
default:
    break
}

Thanks!

technocidal commented 5 years ago

Hi Anthony,

thank you for your response. I will submit a PR.

Thank you for your support