Skip to content

Commit 96b104c

Browse files
committed
Invalid map format handling
1 parent 9e8e3b9 commit 96b104c

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

pytiled_parser/parser.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ def parse_map(file: Path) -> TiledMap:
2121
# The type ignores are because mypy for some reason thinks those functions return Any
2222
if parser == "tmx":
2323
return tmx_map_parse(file) # type: ignore
24-
elif parser == "json":
25-
return json_map_parse(file) # type: ignore
2624
else:
27-
raise UnknownFormat(
28-
"Unknown Map Format, please use either the TMX or JSON format."
29-
)
25+
try:
26+
return json_map_parse(file) # type: ignore
27+
except ValueError:
28+
raise UnknownFormat(
29+
"Unknown Map Format, please use either the TMX or JSON format. "
30+
"This message could also mean your map file is invalid or corrupted."
31+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
|thisissomegarbageformat||||ohmylookhowbaditis||thiswillcertainlybreakthigns|||

tests/test_invalid_format.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
from pathlib import Path
3+
4+
import pytest
5+
6+
from pytiled_parser import UnknownFormat, parse_map
7+
8+
TESTS_DIR = Path(os.path.dirname(os.path.abspath(__file__)))
9+
MAP_FILE = TESTS_DIR / "test_data/invalid_format.garbage"
10+
11+
def test_map_invalid_format():
12+
with pytest.raises(UnknownFormat) as e:
13+
parse_map(MAP_FILE)

0 commit comments

Comments
 (0)