【问题标题】:Error handler not called for promise未要求承诺的错误处理程序
【发布时间】:2016-03-25 23:48:12
【问题描述】:

我有一项服务,当我输入错误的登录凭据时该服务会失败。但是,我的 Promise 错误处理程序没有被调用。

我似乎不明白我的代码出了什么问题,因此永远无法到达 error 回调。

服务

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"
}

【问题讨论】:

    标签: ios iphone swift promisekit


    【解决方案1】:

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

    }.error { error in
    

    与:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 2014-12-28
      • 2015-07-29
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      相关资源
      最近更新 更多