Skip to content

Commit 3176f46

Browse files
akiray03fxdgear
authored andcommitted
TypeError: string indices must be integers in TransportError (elastic#833)
* Add tests of TransportError * Fix to work properly even if the error is not structured on TransportError
1 parent b049b68 commit 3176f46

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

elasticsearch/exceptions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@ def info(self):
5454
def __str__(self):
5555
cause = ''
5656
try:
57-
if self.info:
58-
cause = ', %r' % self.info['error']['root_cause'][0]['reason']
57+
if self.info and 'error' in self.info:
58+
if isinstance(self.info['error'], dict):
59+
cause = ', %r' % self.info['error']['root_cause'][0]['reason']
60+
else:
61+
cause = ', %r' % self.info['error']
5962
except LookupError:
6063
pass
6164
return '%s(%s, %r%s)' % (self.__class__.__name__, self.status_code, self.error, cause)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from elasticsearch.exceptions import TransportError
2+
3+
from .test_cases import TestCase
4+
5+
6+
class TestTransformError(TestCase):
7+
def test_transform_error_parse_with_error_reason(self):
8+
e = TransportError(500, 'InternalServerError', {
9+
'error': {
10+
'root_cause': [
11+
{"type": "error", "reason": "error reason"}
12+
]
13+
}
14+
})
15+
16+
self.assertEqual(str(e), "TransportError(500, 'InternalServerError', 'error reason')")
17+
18+
def test_transform_error_parse_with_error_string(self):
19+
e = TransportError(500, 'InternalServerError', {
20+
'error': 'something error message'
21+
})
22+
23+
self.assertEqual(str(e), "TransportError(500, 'InternalServerError', 'something error message')")

0 commit comments

Comments
 (0)