Skip to content
Merged
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
21 changes: 14 additions & 7 deletions area_reader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,10 @@ def load_sections(self):
logger.info("Processing section %s" % section_name)
try:
readers[section_name]()
except Exception:
self.parse_fail("Error reading section %r" % section_name)
except ParseError:
raise
except Exception as error:
self.parse_fail("Error reading section %r: %r" % (section_name, error))

def load_mobprogs(self):
while True:
Expand Down Expand Up @@ -1944,8 +1946,10 @@ def load_sections(self):
logger.info("Processing section %s" % section_name)
try:
reader()
except Exception:
self.parse_fail("Error reading section %r" % section_name)
except ParseError:
raise
except Exception as error:
self.parse_fail("Error reading section %r: %r" % (section_name, error))

def read_area_metadata(self):
self.area.name = self.read_string()
Expand Down Expand Up @@ -1999,7 +2003,7 @@ def load_smaug_vnum_section(self, section_object_type):
break
if self.current_char != '#':
self.parse_fail("Expected # got %s" % self.current_char)
next_char = self.data[self.index + 1]
next_char = self.data[self.index + 1:self.index + 2]
if not (next_char.isdigit() or next_char == '-'):
break
vnum = self.read_vnum()
Expand Down Expand Up @@ -2098,7 +2102,7 @@ def load_swr_vnum_section(self, section_object_type):
break
if self.current_char != '#':
self.parse_fail("Expected # got %s" % self.current_char)
next_char = self.data[self.index + 1]
next_char = self.data[self.index + 1:self.index + 2]
if not (next_char.isdigit() or next_char == '-'):
break
vnum = self.read_vnum()
Expand Down Expand Up @@ -2843,7 +2847,10 @@ def read_record_header(self):
self.index += 1
if self.current_char == '~':
self.index += 1
return int(token)
try:
return int(token)
except ValueError:
self.parse_fail("Expected numeric record header, got %r" % token)

def read_int_list(self):
line = self.read_line().strip()
Expand Down
71 changes: 71 additions & 0 deletions test_error_reporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Regression tests for issue #12: section readers must not hide the real diagnostic."""

from pathlib import Path

import pytest

import area_reader


def write_area(tmp_path: Path, text: str, name: str = "broken.are") -> Path:
path = tmp_path / name
path.write_text(text, encoding="latin-1")
return path


def test_rom_section_error_keeps_the_specific_parse_message(tmp_path):
path = write_area(tmp_path, "#MOBILES\n#3000\nguard~\nA guard\n")
reader = area_reader.RomAreaFile(path)

with pytest.raises(area_reader.ParseError) as caught:
reader.load_sections()

assert "Unterminated string" in str(caught.value)
assert "Error reading section" not in str(caught.value)


def test_smaug_section_error_keeps_the_specific_parse_message(tmp_path):
path = write_area(tmp_path, "#MOBILES\n#3000\nguard~\nA guard\n")
reader = area_reader.SmaugAreaFile(path)

with pytest.raises(area_reader.ParseError) as caught:
reader.load_sections()

assert "Unterminated string" in str(caught.value)
assert "Error reading section" not in str(caught.value)


def test_non_parse_errors_are_reported_with_the_original_error(tmp_path, monkeypatch):
path = write_area(tmp_path, "#MOBILES\n#0\n\n#$\n")
reader = area_reader.RomAreaFile(path)

def exploding_reader():
raise RuntimeError("internal boom")

monkeypatch.setattr(reader, "load_mobiles", exploding_reader)

with pytest.raises(area_reader.ParseError) as caught:
reader.load_sections()

assert "Error reading section 'mobiles'" in str(caught.value)
assert "internal boom" in str(caught.value)


@pytest.mark.parametrize("reader_class", ["SmaugAreaFile", "SwrAreaFile"])
def test_vnum_section_tolerates_a_trailing_hash_at_end_of_file(tmp_path, reader_class):
path = write_area(tmp_path, "#MOBILES\n#", name="trailing.are")
reader = getattr(area_reader, reader_class)(path)

reader.load_sections()

assert reader.area.mobs == {}


def test_circle_record_header_rejects_a_non_numeric_vnum(tmp_path):
path = tmp_path / "broken.wld"
path.write_text("#\nname~\n", encoding="latin-1")
reader = area_reader.CircleAreaFile(tmp_path)
reader.open_circle_file(path)

with pytest.raises(area_reader.ParseError, match="Expected numeric record header"):
reader.read_record_header()