Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more input context to all error messages. #581

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions montepy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,28 @@ class LineExpansionWarning(Warning):
def __init__(self, message):
self.message = message
super().__init__(self.message)


def add_line_number_to_exception(error, broken_robot):
""" """
try:
input_obj = broken_robot._input
assert input_obj is not None
except (AttributeError, AssertionError) as e:
lineno = "unknown"
file = "unknown"
lines = []
else:
lineno = input_obj.line_number
file = str(input_obj.input_file)
lines = input_obj.input_lines
args = e.args
if len(args) > 0:
message = args[0]
else:
message = ""
message = f"{message}\nError came from line: {lineno} of {file}.\n"
f"{'\n'.join(lines)}"
args = (message,) + args[1:]
e.args = args
raise e
2 changes: 1 addition & 1 deletion montepy/input_parser/input_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ def write(self, to_write):
return self._fh.write(to_write)

def __str__(self):
return self.name
return str(self.name)
14 changes: 13 additions & 1 deletion montepy/mcnp_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,19 @@ def parse_input(self, check_input=False, replace=True):
if isinstance(obj, transform.Transform):
self._transforms.append(obj, False)
if trailing_comment is not None and last_obj is not None:
obj._grab_beginning_comment(trailing_comment)
try:
obj._grab_beginning_comment(trailing_comment)
except Exception as e:
args = e.args
if len(args) > 0:
message = args[0]
else:
message = ""
message = f"{message}\nError came from line: {obj._input.line_number} of {obj._input.input_file}.\n"
message += '\n'.join(obj._input.input_lines)
args = (message,) + args[1:]
e.args = args
raise e
last_obj._delete_trailing_comment()
trailing_comment = obj.trailing_comment
last_obj = obj
Expand Down
Loading