TypeError: string indices must be integers in TransportError (#833)
* Add tests of TransportError * Fix to work properly even if the error is not structured on TransportError
This commit is contained in:
committed by
Nick Lang
parent
b049b684bb
commit
3176f46ae8
@@ -54,8 +54,11 @@ class TransportError(ElasticsearchException):
|
||||
def __str__(self):
|
||||
cause = ''
|
||||
try:
|
||||
if self.info:
|
||||
cause = ', %r' % self.info['error']['root_cause'][0]['reason']
|
||||
if self.info and 'error' in self.info:
|
||||
if isinstance(self.info['error'], dict):
|
||||
cause = ', %r' % self.info['error']['root_cause'][0]['reason']
|
||||
else:
|
||||
cause = ', %r' % self.info['error']
|
||||
except LookupError:
|
||||
pass
|
||||
return '%s(%s, %r%s)' % (self.__class__.__name__, self.status_code, self.error, cause)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
from elasticsearch.exceptions import TransportError
|
||||
|
||||
from .test_cases import TestCase
|
||||
|
||||
|
||||
class TestTransformError(TestCase):
|
||||
def test_transform_error_parse_with_error_reason(self):
|
||||
e = TransportError(500, 'InternalServerError', {
|
||||
'error': {
|
||||
'root_cause': [
|
||||
{"type": "error", "reason": "error reason"}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
self.assertEqual(str(e), "TransportError(500, 'InternalServerError', 'error reason')")
|
||||
|
||||
def test_transform_error_parse_with_error_string(self):
|
||||
e = TransportError(500, 'InternalServerError', {
|
||||
'error': 'something error message'
|
||||
})
|
||||
|
||||
self.assertEqual(str(e), "TransportError(500, 'InternalServerError', 'something error message')")
|
||||
Reference in New Issue
Block a user