errors: InvalidMetadataError stores its key and value.

This makes it easier to introspect than trying to parse the message,
which is meant primarily for human readers.
This commit is contained in:
Brett Smith 2020-03-31 11:50:27 -04:00
parent a9eab2d4ea
commit 7a4e1c52f3
3 changed files with 12 additions and 10 deletions

View file

@ -134,3 +134,5 @@ class InvalidMetadataError(Error):
srcname, key, need_type.__name__, type(value).__name__,
)
super().__init__(msg, txn, source)
self.key = key
self.value = value

View file

@ -37,8 +37,8 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
try:
self._check_links(txn, post, 'receipt')
except errormod.Error as error:
self._check_links(txn, post, self.METADATA_KEY)
except errormod.InvalidMetadataError as error:
receipt_error = error
else:
return
@ -62,11 +62,11 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
try:
self._check_links(txn, post, fallback_key)
except errormod.Error as fallback_error:
if (receipt_error.message.endswith(f" missing {self.METADATA_KEY}")
and fallback_error.message.endswith(f" missing {fallback_key}")):
receipt_error.message += f" or {fallback_key}"
yield receipt_error
except errormod.InvalidMetadataError as fallback_error:
if receipt_error.value is None and fallback_error.value is None:
yield errormod.InvalidMetadataError(
txn, f"{self.METADATA_KEY} or {fallback_key}", None, post,
)
else:
yield receipt_error
yield fallback_error

View file

@ -74,19 +74,19 @@ class MetaReceivableDocumentation(core._RequireLinksPostingMetadataHook):
return self.ISSUED_INVOICE_RE.search(invoice_link) is not None
def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
errors: Dict[MetaKey, Optional[errormod.Error]] = {
errors: Dict[MetaKey, Optional[errormod.InvalidMetadataError]] = {
key: None for key in self.SUPPORTING_METADATA
}
have_support = False
for key in errors:
try:
self._check_links(txn, post, key)
except errormod.Error as key_error:
except errormod.InvalidMetadataError as key_error:
errors[key] = key_error
else:
have_support = True
for key, error in errors.items():
if error and not error.message.endswith(f" missing {key}"):
if error is not None and error.value is not None:
yield error
if not have_support:
yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post)