试图访问Alamofire中的错误代码

时间:2017-03-09 14:55:44

标签: swift http alamofire

我正在使用Alamofire 4.当我这样做时

print(response.debugDescription)

我在控制台中有这样的东西:

[Request]: https://api2.website.com
[Response]: nil
[Data]: 0 bytes
[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x17444ace0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x170490e50 [0x1ab165bb8]>{length = 16, capacity = 16, bytes = 0x100201bb341d1f890000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://api2.flowwow.com/api2/client/info/?auth_token=da88d8aa49ff6f8bb4e1&hash=7f38be3f68db39a6d88687505fdb9ba5&partner_id=1004, NSErrorFailingURLKey=https://api2.website.com, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSLocalizedDescription=The Internet connection appears to be offline.}
[Timeline]: Timeline: { "Request Start Time": 510763454.078, "Initial Response Time": 510763455.293, "Request Completed Time": 510763455.293, "Serialization Completed Time": 510763455.297, "Latency": 1.215 secs, "Request Duration": 1.215 secs, "Serialization Duration": 0.005 secs, "Total Duration": 1.220 secs }

有一条特别的线让我感兴趣:

Error Domain=NSURLErrorDomain Code=-1009

如何获取此代码以便我能正确处理错误。我尝试了所有可以组合的组合,但是在任何地方都没有这个代码的痕迹。

2 个答案:

答案 0 :(得分:2)

当您使用Alamofire拨打电话时,它会返回一个响应,您可以在其中检查是否有任何错误。这是使用Alamofire进行错误处理调用的一个简单示例。

Alamofire.request("https://your.url.com").responseJSON { response in
    if (response.result.isSuccess){
        //do your json stuff
    } else if (response.result.isFailure) {
        //Manager your error
        switch (response.error!._code){
            case NSURLErrorTimedOut:
                //Manager your time out error
                break
            case NSURLErrorNotConnectedToInternet:
                //Manager your not connected to internet error
                break
            default:
                //manager your default case 
            }
    }
}

享受:)

答案 1 :(得分:1)

Alamofire请求示例

from functools import partial

class color:
    colors = {
        "fg":{"black":"30","red": "31","green": "32","yellow": "33","blue": "34","magenta": "35","cyan": "36","white": "37"},
        "bg":{"black":"40","red": "41","green": "42","yellow": "43","blue": "44","magenta": "45","cyan": "46","white": "47"}
    }

    def __init__(self, text):
        self.text = text
        self.bright = "0"
        self.fore = "39"
        self.back = "49"

    def _set(self, name, val):
        # This method just calls __setattr__ to actually change the value,
        # but importantly, it returns self, so the methods can be chained.
        self.__setattr__(name, val)
        return self

    def __getattr__(self, name):
        # Return a partial function with the values already populated
        if name[-2:].lower() == "bg":
            return partial(self._set, "back", self.colors['bg'][name[:-2]])
        elif name == "bold":
            return partial(self._set, "bright", 1)
        return partial(self._set, "fore", self.colors['fg'][name])

    def __repr__(self):
        return f"\033[{self.bright};{self.fore};{self.back}m{self.text}\033[0m"

希望这会有所帮助!