Skip to content

Commit

Permalink
Better debugging messages for database errors.
Browse files Browse the repository at this point in the history
When there are issues in a database file, such as nan values here:
  uncertainty=RateUncertainty(mu=nan, var=nan,
which may be due to a bug in the tree fitting, it was very hard to 
find the cause of the error because the stack trace wouldn't tell you
where the error was, and the file handle had been read by the time you
caught the exception.
With this change, we read the file to a variable, then try executing it,
and catch and report any exceptions properly.
  • Loading branch information
rwest committed May 15, 2024
1 parent 6a5ebf4 commit 55bfc65
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions rmgpy/data/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,17 @@ def load(self, path, local_context=None, global_context=None):
local_context[key] = value

# Process the file
f = open(path, 'r')
with open(path, 'r') as f:
content = f.read()
try:
exec(f.read(), global_context, local_context)
except Exception:
logging.error('Error while reading database {0!r}.'.format(path))
exec(content, global_context, local_context)
except Exception as e:
logging.exception(f'Error while reading database file {path}.')
line_number = e.__traceback__.tb_next.tb_lineno
logging.error(f'Error occurred at or near line {line_number} of {path}.')
lines = content.splitlines()
logging.error(f'Line: {lines[line_number - 1]}')
raise
f.close()

# Extract the database metadata
self.name = local_context['name']
Expand Down

0 comments on commit 55bfc65

Please sign in to comment.