diff --git a/pytiled_parser/parser.py b/pytiled_parser/parser.py index 4dfbcc0..d3704d8 100644 --- a/pytiled_parser/parser.py +++ b/pytiled_parser/parser.py @@ -21,9 +21,11 @@ def parse_map(file: Path) -> TiledMap: # The type ignores are because mypy for some reason thinks those functions return Any if parser == "tmx": return tmx_map_parse(file) # type: ignore - elif parser == "json": - return json_map_parse(file) # type: ignore else: - raise UnknownFormat( - "Unknown Map Format, please use either the TMX or JSON format." - ) + try: + return json_map_parse(file) # type: ignore + except ValueError: + raise UnknownFormat( + "Unknown Map Format, please use either the TMX or JSON format. " + "This message could also mean your map file is invalid or corrupted." + ) diff --git a/tests/test_data/invalid_format.garbage b/tests/test_data/invalid_format.garbage new file mode 100644 index 0000000..ca06863 --- /dev/null +++ b/tests/test_data/invalid_format.garbage @@ -0,0 +1 @@ +|thisissomegarbageformat||||ohmylookhowbaditis||thiswillcertainlybreakthigns||| \ No newline at end of file diff --git a/tests/test_invalid_format.py b/tests/test_invalid_format.py new file mode 100644 index 0000000..a123889 --- /dev/null +++ b/tests/test_invalid_format.py @@ -0,0 +1,13 @@ +import os +from pathlib import Path + +import pytest + +from pytiled_parser import UnknownFormat, parse_map + +TESTS_DIR = Path(os.path.dirname(os.path.abspath(__file__))) +MAP_FILE = TESTS_DIR / "test_data/invalid_format.garbage" + +def test_map_invalid_format(): + with pytest.raises(UnknownFormat) as e: + parse_map(MAP_FILE)