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__, srcname, key, need_type.__name__, type(value).__name__,
) )
super().__init__(msg, txn, source) 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: def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
try: try:
self._check_links(txn, post, 'receipt') self._check_links(txn, post, self.METADATA_KEY)
except errormod.Error as error: except errormod.InvalidMetadataError as error:
receipt_error = error receipt_error = error
else: else:
return return
@ -62,11 +62,11 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
try: try:
self._check_links(txn, post, fallback_key) self._check_links(txn, post, fallback_key)
except errormod.Error as fallback_error: except errormod.InvalidMetadataError as fallback_error:
if (receipt_error.message.endswith(f" missing {self.METADATA_KEY}") if receipt_error.value is None and fallback_error.value is None:
and fallback_error.message.endswith(f" missing {fallback_key}")): yield errormod.InvalidMetadataError(
receipt_error.message += f" or {fallback_key}" txn, f"{self.METADATA_KEY} or {fallback_key}", None, post,
yield receipt_error )
else: else:
yield receipt_error yield receipt_error
yield fallback_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 return self.ISSUED_INVOICE_RE.search(invoice_link) is not None
def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter: 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 key: None for key in self.SUPPORTING_METADATA
} }
have_support = False have_support = False
for key in errors: for key in errors:
try: try:
self._check_links(txn, post, key) self._check_links(txn, post, key)
except errormod.Error as key_error: except errormod.InvalidMetadataError as key_error:
errors[key] = key_error errors[key] = key_error
else: else:
have_support = True have_support = True
for key, error in errors.items(): 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 yield error
if not have_support: if not have_support:
yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post)