Skip to content

Commit

Permalink
Provide more context for import errors
Browse files Browse the repository at this point in the history
If we can't import something, we should provide as much information as
possible: it could be anything from a missing package to a typo.

Fixes #17

Signed-off-by: Stephen Finucane <stephen@that.guru>
  • Loading branch information
stephenfin committed Nov 1, 2017
1 parent 2d9e9f9 commit 6017e03
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions sphinx_click/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from docutils.parsers import rst
from docutils.parsers.rst import directives
from docutils import statemachine
import traceback

import click

Expand Down Expand Up @@ -243,14 +244,21 @@ def _load_module(self, module_path):
try:
module_name, attr_name = module_path.split(':', 1)
except ValueError: # noqa
raise self.error('"{}" is not of format "module.parser"'.format(
raise self.error('"{}" is not of format "module:parser"'.format(
module_path))

try:
mod = __import__(module_name, globals(), locals(), [attr_name])
except: # noqa
raise self.error('Failed to import "{}" from "{}"'.format(
attr_name, module_name))
except (Exception, SystemExit) as exc: # noqa
err_msg = 'Failed to import "{}" from "{}". '.format(
attr_name, module_name)
if isinstance(exc, SystemExit):
err_msg += 'The module appeared to call sys.exit()'
else:
err_msg += 'The following exception was raised:\n{}'.format(
traceback.format_exc())

raise self.error(err_msg)

if not hasattr(mod, attr_name):
raise self.error('Module "{}" has no attribute "{}"'.format(
Expand Down

0 comments on commit 6017e03

Please sign in to comment.