Skip to content

Commit 1d12a39

Browse files
committed
enhance: improve ParsingError to include file name, line number, and content for detailed error messages
1 parent 850a7da commit 1d12a39

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pydifact/exceptions.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,17 @@ class ValidationError(Exception):
1717

1818

1919
class ParsingError(Exception):
20-
pass
20+
def __init__(
21+
self, message: str, file_name: str = "", line_number: int = 0, line: str = ""
22+
):
23+
self.line_number = line_number
24+
self.line = line
25+
self.file_name = file_name
26+
if not line and not line_number:
27+
super().__init__(f"Parsing Error: {message}")
28+
super().__init__(
29+
f"Parsing Error in {file_name}:{line_number}: {message}\n'{line}'"
30+
)
2131

2232

2333
# ---------------- Warnings ----------------

scripts/generator/pydifact_generator.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,10 @@ def save_current_segment():
726726
"""Helper to save the current segment."""
727727
if not segment.tag:
728728
return
729+
730+
# Save any pending top-level element first
731+
save_toplevel_element()
732+
729733
if segment.tag not in segment_specs:
730734
logger.warning(f"Could not fill segment {segment.tag} schema")
731735
else:
@@ -736,6 +740,7 @@ def save_toplevel_element():
736740
"""Helper to save the current top-level element."""
737741
if state.last_toplevel_element:
738742
segment.schema.append(state.last_toplevel_element)
743+
state.last_toplevel_element = None
739744
state.sub_elements = []
740745

741746
while True:
@@ -753,7 +758,8 @@ def save_toplevel_element():
753758
tag, title = title_match
754759

755760
# Save previous segment
756-
save_current_segment()
761+
if state.in_segment:
762+
save_current_segment()
757763

758764
# Stop if we only want one segment and found another
759765
if state.in_segment and only_segment_tag:
@@ -800,7 +806,6 @@ def save_toplevel_element():
800806
break
801807

802808
# Save last segment
803-
save_toplevel_element()
804809
save_current_segment()
805810

806811

0 commit comments

Comments
 (0)