错误处理程序不要求承诺 [英] Error handler not called for promise

查看:93
本文介绍了错误处理程序不要求承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a service, which fails when I enter bad login credentials. However, my Promise error handler does not get called.

我似乎不了解我的代码有什么问题,因此永远不会到达error回调.

I don't seem to grasp what's wrong with my code so that the error callback is never reached.

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不处理取消错误,而错误的凭据恰恰是取消错误.如果将print(e.cancelled)放在reject(e)之前,则会看到它将返回true.例如,如果输入错误的URL,则会收到false.为了解决这个问题,只需替换

By default error does not handle cancellation errors and bad credentials is exactly a cancellation error. If you put print(e.cancelled) before reject(e), you will see that it will return true. If you give a wrong URL for example, you will receive false. In order to get around this, just replace

}.error { error in

具有:

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

然后将触发

error.如果使用recover,默认情况下将处理取消错误.您可以检查 https://github.com/mxcl/PromiseKit/blob/master/Sources/Promise.swift#L367 了解更多信息.

and error will be triggered then. In case you use recover, cancellation errors will be handled by default. You can check https://github.com/mxcl/PromiseKit/blob/master/Sources/Promise.swift#L367 for more information.

这篇关于错误处理程序不要求承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆