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:
source = source[key]
except KeyError as error:
try:
txn_id = f"Transaction {self['transaction_info']['transaction_id']}"
except KeyError:
txn_id = "Transaction"
if source is self._response:
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
else:
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 _load_from_response