From 6fce0c5c2a26b68934ae7b2a2972ef864d68c949 Mon Sep 17 00:00:00 2001 From: Richard West Date: Wed, 15 May 2024 14:39:57 -0400 Subject: [PATCH] Better debugging messages for database errors. 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. --- rmgpy/data/base.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/rmgpy/data/base.py b/rmgpy/data/base.py index d2586a6b30..f166601708 100644 --- a/rmgpy/data/base.py +++ b/rmgpy/data/base.py @@ -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']