Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Objective-c catch block not executed with PMKWhen and Swift Errors #626

Closed
lammertw opened this issue Feb 7, 2017 · 1 comment
Closed

Comments

@lammertw
Copy link
Contributor

lammertw commented Feb 7, 2017

  • PromiseKit version: 4.1.7
  • Xcode version: 8.2.1
  • CocoaPods version: 1.2.0

When bridging from a Swift Promise to an Objective-C AnyPromise the catch block in objc-c is not invoked when a Swift Error is thrown and PMKWhen is used around the AnyPromise.

Example:

Swift:

enum MyError: Error {
    case PromiseError()
}

@objc class MyClass: NSObject {

    @objc class func promise() -> AnyPromise {
        let promise: Promise<String> = Promise(resolvers: { _, reject in
            reject(MyError.PromiseError())
        }).recover(execute: { error -> String in
            print("Swift Error")
            throw error
        })

        return AnyPromise(promise)
    }
}

Objective-c:

    PMKWhen(@[[MyClass promise], [MyClass promise]]).then(^(id value){
        NSLog(@"Success: %@", value);
    }).catch(^(NSError *error) {
        NSLog(@"Error: %@", error);
        @throw error;
    });

Instead of executing the catch block of Objective-c it calls the success block. This print the following:

Swift Error
Swift Error
2017-02-07 13:36:17.450 OrderWriter[37033:1382381] Success: (
    "NyonOrder.MyError.PromiseError",
    "NyonOrder.MyError.PromiseError"
)

A workaround is to rethrow the Swift error as an NSError:

enum MyError: Error {
    case PromiseError()
}

@objc class MyClass: NSObject {

    @objc class func promise() -> AnyPromise {
        let promise: Promise<String> = Promise(resolvers: { _, reject in
            reject(MyError.PromiseError())
        }).recover(execute: { error -> String in
            print("Swift Error")
            throw NSError(domain: "myerror", code: 0, userInfo: nil) // throw NSError instead
        })

        return AnyPromise(promise)
    }
}

In that case the catch block is executed and will print the following:

Swift Error
Swift Error
2017-02-07 13:39:54.612 OrderWriter[37437:1403750] Error: Error Domain=myerror Code=0 "(null)" UserInfo={PMKFailingPromiseIndexKey=0, NSUnderlyingError=0x61800005a520 {Error Domain=myerror Code=0 "(null)"}}
@mxcl
Copy link
Owner

mxcl commented Feb 7, 2017

Hmm, looks like quite a bug. Sorry.

@mxcl mxcl closed this as completed in cfae62d Apr 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants