error handling程序未被调用promise

我有一个服务,当我input错误的login凭据失败。 但是,我的承诺error handling程序不会被调用。

我似乎并没有明白我的代码有什么问题,所以errorcallback从来没有达到过。

服务

 func loadRepositories() -> Promise<[Repository]>{ return Promise { fullfill, reject in manager.request(Method.GET, baseURL + "/api/1.0/user/repositories") .authenticate(user: username, password: password) .responseArray { (response: Response<[Repository], NSError>) in switch response.result{ case .Success(let value): fullfill(value) case .Failure(let e): // The breakpoint here is reached. reject(e) } } } } 

处理

 firstly{ service!.loadRepositories() }.then { repositories -> Void in loginVC.dismissViewControllerAnimated(true, completion: nil) self.onLoginSuccessful() }.always{ // Always gets called loginVC.isSigningIn = false }.error { error in // I never get here even though `reject(e)` is called from the service. loginVC.errorString = "Login went wrong" } 

默认情况下, error不会处理取消错误,错误的凭据就是取消错误。 如果在reject(e) print(e.cancelled)之前放置print(e.cancelled) ,则会看到它将返回true 。 例如,如果您给出错误的URL,您将收到false 。 为了解决这个问题,只需更换

 }.error { error in 

有:

 }.error(policy: .AllErrors) { error in 

然后会触发error 。 如果您使用recover ,取消错误将被默认处理。 你可以查看https://github.com/mxcl/PromiseKit/blob/master/Sources/Promise.swift#L367了解更多信息&#x3002;

Interesting Posts