Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Iterator next中优雅地捕获python异常_Python_Exception_Pygithub - Fatal编程技术网

在Iterator next中优雅地捕获python异常

在Iterator next中优雅地捕获python异常,python,exception,pygithub,Python,Exception,Pygithub,我正在使用PyGithub刮取一些存储库,尽管在遍历搜索页面时出现了一些错误 def scrape_interval(self, interval): for repo_number, repo in self.search(interval): code... def search(self, interval): try: iterator = enumerate(self.github.search_repositories(query="

我正在使用PyGithub刮取一些存储库,尽管在遍历搜索页面时出现了一些错误

def scrape_interval(self, interval):
    for repo_number, repo in self.search(interval):
        code...

def search(self, interval):
    try:
        iterator = enumerate(self.github.search_repositories(query="Laravel created:" + interval))
    except:
        print.warning("Going to sleep for 1 hour. The search API hit the limit")
        time.sleep(3600)
        iterator = self.search(interval)
    return iterator
如您所见,在
def search
中创建迭代器时,我试图捕捉错误。但是在self.search(interval)中的repo\u number,repo的第
行抛出了错误:
所以迭代器获取下一项的某个时刻

我有什么办法可以让这些错误被捕获?我最好避免将整个for循环包装在try子句中,而不是在迭代过程中对其进行管理

有关错误本身的参考信息:

File "/Users/olofjondelius/Documents/Code/laravel-ai/src/examples/migration-analysis/../../GithubScraper.py", line 47, in scrape_interval
    for repo_number, repo in self.search(interval):
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/PaginatedList.py", line 58, in _iter_
    newElements = self._grow()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/PaginatedList.py", line 70, in _grow
    newElements = self._fetchNextPage()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/PaginatedList.py", line 172, in _fetchNextPage
    headers=self.__headers
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 185, in requestJsonAndCheck
    return self.__check(*self.requestJson(verb, url, parameters, headers, input, cnx))
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 231, in requestJson
    return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 284, in __requestEncode
    status, responseHeaders, output = self.__requestRaw(cnx, verb, url, requestHeaders, encoded_input)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/site-packages/github/Requester.py", line 309, in __requestRaw
    requestHeaders
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1016, in _send_output
    self.send(msg)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 956, in send
    self.connect()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 1384, in connect
    super().connect()
  File "/anaconda3/envs/laravel-ai/lib/python3.7/http/client.py", line 928, in connect
    (self.host,self.port), self.timeout, self.source_address)
  File "/anaconda3/envs/laravel-ai/lib/python3.7/socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/anaconda3/envs/laravel-ai/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known

听起来像是在迭代迭代器时引发异常,而不是在创建迭代器时。当前的
try
except
块仅捕获调用
self.github.search\u存储库时立即引发的异常,而不是在使用结果时出现的任何异常

要解决这个问题,您可以将
搜索
函数设置为生成器。这将允许您在有值的情况下生成值,但仍然可以捕获异常并根据需要经常重试

试着这样做:

def search(self, interval):
    while True:
        try:
            it = enumerate(self.github.search_repositories(query="Laravel created:" + interval))
            yield from it
            return   # if we completed the yield from without an exception, we're done!

        except:  # you should probably limit this to catching a specific exception types
            print.warning("Going to sleep for 1 hour. The search API hit the limit")
            time.sleep(3600)

正如我在一篇评论中指出的,您可能应该将裸露的
except
语句更改为
except socket.gairror
或类似的语句,这样您就不会抑制所有异常,而是只抑制您期望的异常,并且延迟将为您修复。仍然应该允许某些真正出乎意料的事情来停止程序(因为它可能反映了代码中其他地方的错误)。

关于设置异常回溯格式的建议:请使用代码格式,而不是引用格式,即使这样会导致长行。引号格式使文本行回流,将调用位置和代码混合在一起,形成一团混乱。与某些格式问题不同,它不容易修复。知道Python回溯应该是什么样子的人可以手动重新格式化它(在正确的位置插入换行符,并尽可能地缩进每一行),但这是一个巨大的麻烦,特别是对于像您这样有十几个调用深度的回溯。@Blckknght非常好的提示-谢谢!更新。纯美,谢谢!要到22小时才能赏金。