Refactor the detailed error messages

This commit is contained in:
James R. Barlow
2018-10-04 00:10:59 -07:00
parent b32dd9f9d3
commit c74f2ee6e8
2 changed files with 37 additions and 26 deletions
+10 -26
View File
@@ -628,6 +628,13 @@ def do_ruffus_exception(ruffus_five_tuple, options, log):
base_exc_name = exc_name.replace('ocrmypdf.exceptions.', '') base_exc_name = exc_name.replace('ocrmypdf.exceptions.', '')
exc_class = getattr(ocrmypdf_exceptions, base_exc_name) exc_class = getattr(ocrmypdf_exceptions, base_exc_name)
exit_code = getattr(exc_class, 'exit_code', ExitCode.other_error) exit_code = getattr(exc_class, 'exit_code', ExitCode.other_error)
try:
if isinstance(exc_value, exc_class):
exc_msg = str(exc_value)
else:
exc_msg = str(exc_class())
except Exception:
exc_msg = "Unknown"
if exc_name in ('builtins.SystemExit', 'SystemExit'): if exc_name in ('builtins.SystemExit', 'SystemExit'):
match = re.search(r"\.(.+?)\)", exc_value) match = re.search(r"\.(.+?)\)", exc_value)
@@ -645,32 +652,9 @@ def do_ruffus_exception(ruffus_five_tuple, options, log):
msg = "Error occurred while running this command:" msg = "Error occurred while running this command:"
log.error(msg + '\n' + exc_value) log.error(msg + '\n' + exc_value)
exit_code = ExitCode.child_process_error exit_code = ExitCode.child_process_error
elif exc_name == 'ocrmypdf.exceptions.EncryptedPdfError': elif exc_name.startswith('ocrmypdf.exceptions.'):
log.error(textwrap.dedent("""\ if exc_msg:
Input PDF is encrypted. The encryption must be removed to log.error(exc_msg)
perform OCR.
For information about this PDF's security use
qpdf --show-encryption infilename
You can remove the encryption using
qpdf --decrypt [--password=[password]] infilename
"""))
elif exc_name == 'ocrmypdf.exceptions.PdfMergeFailedError':
log.error(textwrap.dedent("""\
Failed to merge PDF image layer with OCR layer
Usually this happens because the input PDF file is mal-formed and
ocrmypdf cannot automatically correct the problem on its own.
Try using
ocrmypdf --pdf-renderer sandwich [..other args..]
"""))
elif exc_name == 'ocrmypdf.exceptions.TesseractConfigError':
log.error(textwrap.dedent("""\
Error occurred while parsing a tesseract configuration file
"""))
elif exc_name == 'PIL.Image.DecompressionBombError': elif exc_name == 'PIL.Image.DecompressionBombError':
msg = cleanup_ruffus_error_message(exc_value) msg = cleanup_ruffus_error_message(exc_value)
msg += ("\nUse the --max-image-mpixels argument to set increase the " msg += ("\nUse the --max-image-mpixels argument to set increase the "
+27
View File
@@ -17,6 +17,7 @@
from enum import IntEnum from enum import IntEnum
from textwrap import dedent
class ExitCode(IntEnum): class ExitCode(IntEnum):
ok = 0 ok = 0
@@ -36,6 +37,13 @@ class ExitCode(IntEnum):
class ExitCodeException(Exception): class ExitCodeException(Exception):
exit_code = ExitCode.other_error exit_code = ExitCode.other_error
message = ""
def __str__(self):
super_msg = super().__str__() # Don't do str(super())
if self.message:
return self.message.format(super_msg)
return super_msg
class BadArgsError(ExitCodeException): class BadArgsError(ExitCodeException):
@@ -44,7 +52,15 @@ class BadArgsError(ExitCodeException):
class PdfMergeFailedError(ExitCodeException): class PdfMergeFailedError(ExitCodeException):
exit_code = ExitCode.input_file exit_code = ExitCode.input_file
message = dedent('''\
Failed to merge PDF image layer with OCR layer
Usually this happens because the input PDF file is malformed and
ocrmypdf cannot automatically correct the problem on its own.
Try using
ocrmypdf --pdf-renderer sandwich [..other args..]
''')
class MissingDependencyError(ExitCodeException): class MissingDependencyError(ExitCodeException):
exit_code = ExitCode.missing_dependency exit_code = ExitCode.missing_dependency
@@ -76,7 +92,18 @@ class SubprocessOutputError(ExitCodeException):
class EncryptedPdfError(ExitCodeException): class EncryptedPdfError(ExitCodeException):
exit_code = ExitCode.encrypted_pdf exit_code = ExitCode.encrypted_pdf
message = dedent('''\
Input PDF is encrypted. The encryption must be removed to
perform OCR.
For information about this PDF's security use
qpdf --show-encryption infilename
You can remove the encryption using
qpdf --decrypt [--password=[password]] infilename
''')
class TesseractConfigError(ExitCodeException): class TesseractConfigError(ExitCodeException):
exit_code = ExitCode.invalid_config exit_code = ExitCode.invalid_config
message = "Error occurred while parsing a Tesseract configuration file"