Skip to content

Commit

Permalink
[core] Add file name and line number to module loading errors.
Browse files Browse the repository at this point in the history
This should make it easier to debug when the import fails due to some weird error deep inside some module.
  • Loading branch information
ari-koivula committed Jul 7, 2013
1 parent bfcc3e0 commit a8c13cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 9 additions & 3 deletions willie/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ def setup(self):
module = imp.load_source(name, filename)
except Exception, e:
error_count = error_count + 1
stderr("Error loading %s: %s (in bot.py)" % (name, e))
filename, lineno = tools.get_raising_file_and_line()
rel_path = os.path.relpath(filename, os.path.dirname(__file__))
raising_stmt = "%s:%d" % (rel_path, lineno)
stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt))
else:
try:
if hasattr(module, 'setup'):
Expand All @@ -271,8 +274,11 @@ def setup(self):
modules.append(name)
except Exception, e:
error_count = error_count + 1
stderr("Error in %s setup procedure: %s (in bot.py)"
% (name, e))
filename, lineno = tools.get_raising_file_and_line()
rel_path = os.path.relpath(filename, os.path.dirname(__file__))
raising_stmt = "%s:%d" % (rel_path, lineno)
stderr("Error in %s setup procedure: %s (%s)"
% (name, e, raising_stmt))

if modules:
stderr('\n\nRegistered %d modules,' % (len(modules) - 1))
Expand Down
13 changes: 13 additions & 0 deletions willie/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@
import copy


def get_raising_file_and_line(tb=None):
"""Return the file and line number of the statement that raised the tb
Returns: (filename, lineno) tuple
"""
if not tb:
tb = sys.exc_info()[2]

filename, lineno, _context, _line = traceback.extract_tb(tb)[-1]

return filename, lineno


def get_command_regexp(prefix, command):
"""Return a compiled regexp object that implements the command."""
# Escape all whitespace with a single backslash. This ensures that regexp
Expand Down

0 comments on commit a8c13cc

Please sign in to comment.