Skip to content

Commit

Permalink
improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cleder committed Jan 29, 2024
1 parent 5839fe3 commit 44c8811
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions fastkml/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def handle_invalid_geometry_error(
).decode(
"UTF-8",
)
msg = f"Invalid coordinates in {error_in_xml}"
msg = f"Invalid coordinates in '{error_in_xml}' caused by '{error}'"
logger.error(msg)
if strict:
raise KMLParseError(msg) from error
Expand Down Expand Up @@ -706,5 +706,5 @@ def create_kml_geometry(
geometry=geom,
),
)
msg = f"Unsupported geometry type {type(geometry)}"
raise KMLWriteError(msg)
msg = f"Unsupported geometry type {type(geometry)}" # pragma: no cover
raise KMLWriteError(msg) # pragma: no cover
8 changes: 8 additions & 0 deletions tests/geometries/geometry_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,14 @@ def test_create_kml_geometry_geometrycollection_roundtrip(self) -> None:

assert mg.geometry == gc

def test_create_kml_geometry_non_geometry(self) -> None:
"""Test the create_kml_geometry function."""
with pytest.raises(
AttributeError,
match="^'str' object has no attribute '__geo_interface__'$",
):
create_kml_geometry("not a geometry") # type: ignore[arg-type]


class TestGetGeometryLxml(Lxml, TestGetGeometry):
"""Test with lxml."""
Expand Down
13 changes: 8 additions & 5 deletions tests/geometries/point_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ def test_from_string_3d(self) -> None:

def test_empty_from_string(self) -> None:
"""Test the from_string method."""
with pytest.raises(KMLParseError, match=r"Invalid coordinates in <Point\s*/>"):
with pytest.raises(
KMLParseError,
match=r"^Invalid coordinates in '<Point\s*/>",
):
Point.class_from_string(
"<Point/>",
ns="",
Expand All @@ -105,7 +108,7 @@ def test_empty_from_string(self) -> None:
def test_from_string_empty_coordinates(self) -> None:
with pytest.raises(
KMLParseError,
match=r"Invalid coordinates in <Point\s*><coordinates\s*/></Point>",
match=r"^Invalid coordinates in '<Point\s*><coordinates\s*/></Point>",
):
Point.class_from_string(
"<Point><coordinates/></Point>",
Expand All @@ -116,7 +119,7 @@ def test_from_string_invalid_coordinates(self) -> None:
with pytest.raises(
KMLParseError,
match=(
r"Invalid coordinates in <Point\s*>"
r"^Invalid coordinates in '<Point\s*>"
"<coordinates>1</coordinates></Point>"
),
):
Expand All @@ -129,7 +132,7 @@ def test_from_string_invalid_coordinates_4d(self) -> None:
with pytest.raises(
KMLParseError,
match=(
r"Invalid coordinates in <Point\s*>"
r"^Invalid coordinates in '<Point\s*>"
"<coordinates>1,2,3,4</coordinates></Point>"
),
):
Expand All @@ -142,7 +145,7 @@ def test_from_string_invalid_coordinates_non_numerical(self) -> None:
with pytest.raises(
KMLParseError,
match=(
r"Invalid coordinates in <Point\s*>"
r"^Invalid coordinates in '<Point\s*>"
"<coordinates>a,b,c</coordinates></Point>"
),
):
Expand Down

0 comments on commit 44c8811

Please sign in to comment.