transaction: KeyError messages include transaction ID when available.

This streamlines the debugging process for transactions that don't
follow the expected format.
This commit is contained in:
Brett Smith 2020-11-18 16:27:47 -05:00
parent 2ff353bebb
commit 63497d18d4

View file

@ -103,13 +103,17 @@ class Transaction(APIResponse):
try: try:
source = source[key] source = source[key]
except KeyError as error: except KeyError as error:
try:
txn_id = f"Transaction {self['transaction_info']['transaction_id']}"
except KeyError:
txn_id = "Transaction"
if source is self._response: if source is self._response:
raise errors.MissingFieldError( raise errors.MissingFieldError(
f"transaction was not loaded with {error.args[0]!r} field", f"{txn_id} was not loaded with {error.args[0]!r} field",
) from None ) from None
else: else:
key_s = ''.join(repr(key) for key in keys[:index + 1]) key_s = ''.join(repr(key) for key in keys[:index + 1])
raise KeyError(f"Transaction {key_s}") from None raise KeyError(f"{txn_id} {key_s}") from None
return func(source) return func(source)
return _load_from_response return _load_from_response