Skip to content

Commit

Permalink
Improve error feedback of importing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
wajdikhattel committed May 21, 2020
1 parent 9d739f8 commit c24f45f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions textract/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,28 @@ def process(filename, input_encoding=None, output_encoding=DEFAULT_OUTPUT_ENCODI
# the _parser extension
rel_module = ext + _FILENAME_SUFFIX

# If we can't import the module, the file extension isn't currently
# supported
# check if we can import the parser module related to the file extension
try:
filetype_module = importlib.import_module(
rel_module, 'textract.parsers'
)
except ImportError:
raise exceptions.ExtensionNotSupported(ext)
# check if the module exists in the system
is_module = importlib.util.find_spec(
rel_module, package='textract.parsers')

if is_module is not None:
filetype_module = importlib.import_module(
rel_module, 'textract.parsers'
)
else:
# If we can't import the module, the file extension isn't currently
# supported
raise exceptions.ExtensionNotSupported(ext)
except ModuleNotFoundError as absoluteImportFailed:
# Raise the exception of the absolute import failure
raise absoluteImportFailed
except ImportError as relativeImportFailed:
# Raise the exception of the relative import failure
raise relativeImportFailed

# do the extraction

parser = filetype_module.Parser()
return parser.process(filename, input_encoding, output_encoding, **kwargs)

Expand Down

0 comments on commit c24f45f

Please sign in to comment.