diff options
-rw-r--r-- | swiftclient/service.py | 20 | ||||
-rw-r--r-- | test/unit/test_service.py | 16 |
2 files changed, 27 insertions, 9 deletions
diff --git a/swiftclient/service.py b/swiftclient/service.py index d1c98d6..e905ea6 100644 --- a/swiftclient/service.py +++ b/swiftclient/service.py @@ -419,6 +419,9 @@ class _SwiftReader: def __init__(self, path, body, headers, checksum=True): self._path = path self._body = body + self._txn_id = headers.get('x-openstack-request-id') + if self._txn_id is None: + self._txn_id = headers.get('x-trans-id') self._actual_read = 0 self._content_length = None self._actual_md5 = None @@ -459,17 +462,20 @@ class _SwiftReader: def _check_contents(self): if (self._content_length is not None and self._actual_read != self._content_length): - raise SwiftError('Error downloading {0}: read_length != ' - 'content_length, {1:d} != {2:d}'.format( - self._path, self._actual_read, - self._content_length)) + raise SwiftError( + 'Error downloading {0}: read_length != content_length, ' + '{1:d} != {2:d} (txn: {3})'.format( + self._path, self._actual_read, self._content_length, + self._txn_id or 'unknown')) if self._actual_md5 and self._expected_md5: etag = self._actual_md5.hexdigest() if etag != self._expected_md5: - raise SwiftError('Error downloading {0}: md5sum != etag, ' - '{1} != {2}'.format( - self._path, etag, self._expected_md5)) + raise SwiftError( + 'Error downloading {0}: md5sum != etag, ' + '{1} != {2} (txn: {3})'.format( + self._path, etag, self._expected_md5, + self._txn_id or 'unknown')) def bytes_read(self): return self._actual_read diff --git a/test/unit/test_service.py b/test/unit/test_service.py index b6db22d..cdab61f 100644 --- a/test/unit/test_service.py +++ b/test/unit/test_service.py @@ -201,7 +201,19 @@ class TestSwiftReader(unittest.TestCase): with self.assertRaises(SwiftError) as cm: _consume(sr) self.assertEqual( - "'Error downloading path: read_length != content_length, 4 != 5'", + "'Error downloading path: read_length != content_length, " + "4 != 5 (txn: unknown)'", + str(cm.exception)) + + # Check error includes txn id if available + sr = self.sr('path', BytesIO(b'body'), {'content-length': 5, + 'etag': 'bad etag', + 'x-trans-id': 'uuid'}) + with self.assertRaises(SwiftError) as cm: + _consume(sr) + self.assertEqual( + "'Error downloading path: read_length != content_length, " + "4 != 5 (txn: uuid)'", str(cm.exception)) # Check error is raised if SwiftReader doesn't calculate the expected @@ -212,7 +224,7 @@ class TestSwiftReader(unittest.TestCase): _consume(sr) self.assertEqual( "'Error downloading path: md5sum != etag, " - "841a2d689ad86bd1611447453c22c6fc != bad etag'", + "841a2d689ad86bd1611447453c22c6fc != bad etag (txn: unknown)'", str(cm.exception)) sr = self.sr('path', BytesIO(b'body'), {'content-length': 4}) |