AuthorizationException:扫描时出现<exception str()=“”failed =“”>

时间:2016-09-02 20:11:35

标签: python elasticsearch elasticsearch-py

我正在尝试运行看起来非常简单的扫描,但我遇到了一个奇怪的异常。看起来扫描创建的生成器遇到了异常,我不明白为什么。我尝试在try / except中包装for循环的内部但它仍然抛出异常,所以我猜测它在命中es时会死掉。如果您能帮助确定正在发生的事情,我可以获得以下内容:

old_stuff_query = {"query": {"filtered": {
    "filter": {
        "range": {
            "expire_on": {'lte': datetime.datetime.now() - datetime.timedelta(weeks=53)}}

    }}}}

scanResp = scan(client=es, query=old_stuff_query, scroll="1m", index="myIndex", doc_type="myDoc")
counter = 0
for resp in scanResp:
    try:
        print("YAH")
    except:
        print("BOO")

但我得到以下内容:

    for resp in scanResp:
  File "/home/will/.virtualenvs/side_project/local/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 314, in scan
    client.clear_scroll(body={'scroll_id': [scroll_id]}, ignore=(404, ))
  File "/home/will/.virtualenvs/side_project/local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 69, in _wrapped
    return func(*args, params=params, **kwargs)
  File "/home/will/.virtualenvs/side_project/local/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 688, in clear_scroll
    params=params, body=body)
  File "/home/will/.virtualenvs/side_project/local/lib/python2.7/site-packages/elasticsearch/transport.py", line 327, in perform_request
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
  File "/home/will/.virtualenvs/side_project/local/lib/python2.7/site-packages/elasticsearch/connection/http_requests.py", line 84, in perform_request
    self._raise_error(response.status_code, raw_data)
  File "/home/will/.virtualenvs/side_project/local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 113, in _raise_error
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.AuthorizationException: <exception str() failed>

最令人沮丧的部分是这完全是随机发生的。在少于10万个文档和超过250,000个文档之后,它出错了。任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:2)

所以我也遇到了同样的错误并找到了原因: 如果您创建一个具有特殊__str__的自己的类,并且在此方法或其他被调用的方法中,它会尝试生成一个字符串,则该错误消息将与'&lt; exception交换出来str()失败&gt;':

>>> class X:
...     def __str__(self):
...         raise ValueError(self)
... 
>>> print(X())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __str__
ValueError: <exception str() failed>

在你的情况下,它似乎是在api方面的草率编程。