Reputation: 1817
I'm currently mixing SwiftyStoreKit and PromiseKit in order to handle in-app purchases, the problem/issue I'm having is that within the chain of promises if I throw an error within one of them the catch block isn't being executed/hit when the reject()
function is called.
To how I'm chaining these promises, you can see this below.
firstly {
IAPViewModel.retrieveIAPProducts()
}.then { products in
IAPViewModel.purchase(products[1])
}.ensure {
// This is to silence the warning of values being unused
_ = IAPViewModel.validatePurchases()
}.catch { error in
UIAlertController.show(message: "Error - \(error._code): \(error.localizedDescription)")
}
An example of a function that's wrapped around a Promise, the best example is probably my purchase function since users can hit cancel and this will throw an error. See below.
static func purchase(_ product: SKProduct) -> Promise<Void> {
let loftyLoadingViewContentModel = LoftyLoadingViewContentModel(title: "Purchasing".uppercased(), message: "We’re currently processing your\nrequest, for your subscription.")
UIApplication.topViewController()?.showLoadingView(.popOverScreen, loftyLoadingViewContentModel)
return Promise { seal in
SwiftyStoreKit.purchaseProduct(product) { purchaseResult in
switch purchaseResult {
case .success(let product):
if product.needsFinishTransaction {
SwiftyStoreKit.finishTransaction(product.transaction)
}
seal.fulfill()
log.info("Purchase Success: \(product.productId)")
case .error(let error):
UIApplication.topViewController()?.removeLoadingView()
seal.reject(error)
}
}
}
}
I have put a breakpoint in and the error case is being hit when I touch cancel, but this then doesn't trigger the catch block back within the chain of Promises. I can't seem to put my finger on why.
Upvotes: 1
Views: 457
Reputation: 1817
Managed to figured it out, I had to explicitly set that I wanted my catch to catch all errors by adding this parameter to the block, (policy: .allErrors)
.
firstly {
IAPViewModel.retrieveIAPProducts()
}.then { products in
IAPViewModel.purchase(products[1])
}.ensure {
// This is to silence the warning of values being unused
_ = IAPViewModel.validatePurchases()
}.catch (policy: .allErrors) { error in
UIAlertController.show(message: "Error - \(error._code): \(error.localizedDescription)")
}
Upvotes: 1