Merge pull request #1354 from paperless-ngx/bugfix-exception-chains

Bugfix: Chain exceptions during exception handling
This commit is contained in:
Quinn Casey 2022-08-03 09:59:26 -07:00 committed by GitHub
commit 173934258c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 15 deletions

View File

@ -101,8 +101,8 @@ class DocumentClassifier:
self.correspondent_classifier = pickle.load(f) self.correspondent_classifier = pickle.load(f)
self.document_type_classifier = pickle.load(f) self.document_type_classifier = pickle.load(f)
self.storage_path_classifier = pickle.load(f) self.storage_path_classifier = pickle.load(f)
except Exception: except Exception as err:
raise ClassifierModelCorruptError() raise ClassifierModelCorruptError() from err
# Check for the warning about unpickling from differing versions # Check for the warning about unpickling from differing versions
# and consider it incompatible # and consider it incompatible

View File

@ -78,10 +78,16 @@ class Consumer(LoggingMixin):
{"type": "status_update", "data": payload}, {"type": "status_update", "data": payload},
) )
def _fail(self, message, log_message=None, exc_info=None): def _fail(
self,
message,
log_message=None,
exc_info=None,
exception: Optional[Exception] = None,
):
self._send_progress(100, 100, "FAILED", message) self._send_progress(100, 100, "FAILED", message)
self.log("error", log_message or message, exc_info=exc_info) self.log("error", log_message or message, exc_info=exc_info)
raise ConsumerError(f"{self.filename}: {log_message or message}") raise ConsumerError(f"{self.filename}: {log_message or message}") from exception
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -152,6 +158,7 @@ class Consumer(LoggingMixin):
MESSAGE_PRE_CONSUME_SCRIPT_ERROR, MESSAGE_PRE_CONSUME_SCRIPT_ERROR,
f"Error while executing pre-consume script: {e}", f"Error while executing pre-consume script: {e}",
exc_info=True, exc_info=True,
exception=e,
) )
def run_post_consume_script(self, document): def run_post_consume_script(self, document):
@ -217,6 +224,7 @@ class Consumer(LoggingMixin):
MESSAGE_POST_CONSUME_SCRIPT_ERROR, MESSAGE_POST_CONSUME_SCRIPT_ERROR,
f"Error while executing post-consume script: {e}", f"Error while executing post-consume script: {e}",
exc_info=True, exc_info=True,
exception=e,
) )
def try_consume_file( def try_consume_file(
@ -331,6 +339,7 @@ class Consumer(LoggingMixin):
str(e), str(e),
f"Error while consuming document {self.filename}: {e}", f"Error while consuming document {self.filename}: {e}",
exc_info=True, exc_info=True,
exception=e,
) )
# Prepare the document classifier. # Prepare the document classifier.
@ -415,6 +424,7 @@ class Consumer(LoggingMixin):
f"The following error occurred while consuming " f"The following error occurred while consuming "
f"{self.filename}: {e}", f"{self.filename}: {e}",
exc_info=True, exc_info=True,
exception=e,
) )
finally: finally:
document_parser.cleanup() document_parser.cleanup()

View File

@ -197,12 +197,14 @@ class MailAccountHandler(LoggingMixin):
# Need to transition out of AUTH state to SELECTED # Need to transition out of AUTH state to SELECTED
M.folder.set("INBOX") M.folder.set("INBOX")
except Exception: except Exception as err:
self.log( self.log(
"error", "error",
"Unable to authenticate with mail server using AUTH=PLAIN", "Unable to authenticate with mail server using AUTH=PLAIN",
) )
raise MailError(f"Error while authenticating account {account}") raise MailError(
f"Error while authenticating account {account}",
) from err
except Exception as e: except Exception as e:
self.log( self.log(
"error", "error",
@ -245,7 +247,7 @@ class MailAccountHandler(LoggingMixin):
try: try:
M.folder.set(rule.folder) M.folder.set(rule.folder)
except MailboxFolderSelectError: except MailboxFolderSelectError as err:
self.log( self.log(
"error", "error",
@ -264,7 +266,7 @@ class MailAccountHandler(LoggingMixin):
raise MailError( raise MailError(
f"Rule {rule}: Folder {rule.folder} " f"Rule {rule}: Folder {rule.folder} "
f"does not exist in account {rule.account}", f"does not exist in account {rule.account}",
) ) from err
criterias = make_criterias(rule) criterias = make_criterias(rule)
@ -279,8 +281,10 @@ class MailAccountHandler(LoggingMixin):
mark_seen=False, mark_seen=False,
charset=rule.account.character_set, charset=rule.account.character_set,
) )
except Exception: except Exception as err:
raise MailError(f"Rule {rule}: Error while fetching folder {rule.folder}") raise MailError(
f"Rule {rule}: Error while fetching folder {rule.folder}",
) from err
post_consume_messages = [] post_consume_messages = []
@ -320,7 +324,7 @@ class MailAccountHandler(LoggingMixin):
except Exception as e: except Exception as e:
raise MailError( raise MailError(
f"Rule {rule}: Error while processing post-consume actions: " f"{e}", f"Rule {rule}: Error while processing post-consume actions: " f"{e}",
) ) from e
return total_processed_files return total_processed_files

View File

@ -323,11 +323,11 @@ class RasterisedDocumentParser(DocumentParser):
except Exception as e: except Exception as e:
# If this fails, we have a serious issue at hand. # If this fails, we have a serious issue at hand.
raise ParseError(f"{e.__class__.__name__}: {str(e)}") raise ParseError(f"{e.__class__.__name__}: {str(e)}") from e
except Exception as e: except Exception as e:
# Anything else is probably serious. # Anything else is probably serious.
raise ParseError(f"{e.__class__.__name__}: {str(e)}") raise ParseError(f"{e.__class__.__name__}: {str(e)}") from e
# As a last resort, if we still don't have any text for any reason, # As a last resort, if we still don't have any text for any reason,
# try to extract the text from the original document. # try to extract the text from the original document.

View File

@ -57,7 +57,7 @@ class TikaDocumentParser(DocumentParser):
raise ParseError( raise ParseError(
f"Could not parse {document_path} with tika server at " f"Could not parse {document_path} with tika server at "
f"{tika_server}: {err}", f"{tika_server}: {err}",
) ) from err
self.text = parsed["content"].strip() self.text = parsed["content"].strip()
@ -90,7 +90,9 @@ class TikaDocumentParser(DocumentParser):
response = requests.post(url, files=files, headers=headers) response = requests.post(url, files=files, headers=headers)
response.raise_for_status() # ensure we notice bad responses response.raise_for_status() # ensure we notice bad responses
except Exception as err: except Exception as err:
raise ParseError(f"Error while converting document to PDF: {err}") raise ParseError(
f"Error while converting document to PDF: {err}",
) from err
with open(pdf_path, "wb") as file: with open(pdf_path, "wb") as file:
file.write(response.content) file.write(response.content)