diff --git a/api/routers/edr.py b/api/routers/edr.py index db6ee006..6724b7af 100644 --- a/api/routers/edr.py +++ b/api/routers/edr.py @@ -15,6 +15,7 @@ from shapely import buffer from shapely import geometry from shapely import wkt +from shapely.errors import GEOSException router = APIRouter(prefix="/collections/observations") @@ -100,8 +101,14 @@ async def get_data_position( point = wkt.loads(coords) assert point.geom_type == "Point" poly = buffer(point, 0.0001, quad_segs=1) # Roughly 10 meters around the point + except GEOSException: + raise HTTPException(status_code=422, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"}) + except AssertionError: + raise HTTPException(status_code=422, detail={"coords": f"Invalid geometric type: {point.geom_type}"}) except Exception: - raise HTTPException(status_code=422, detail={"coords": "Invalid coordinates: {}".format(coords)}) + raise HTTPException( + status_code=422, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"} + ) return await get_data_area(poly.wkt, parameter_name, datetime, f) @@ -121,8 +128,14 @@ async def get_data_area( try: poly = wkt.loads(coords) assert poly.geom_type == "Polygon" + except GEOSException: + raise HTTPException(status_code=422, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"}) + except AssertionError: + raise HTTPException(status_code=422, detail={"coords": f"Invalid geometric type: {poly.geom_type}"}) except Exception: - raise HTTPException(status_code=422, detail={"coords": "Invalid coordinates: {}".format(coords)}) + raise HTTPException( + status_code=422, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"} + ) range = get_datetime_range(datetime) get_obs_request = dstore.GetObsRequest( diff --git a/api/test/test_edr.py b/api/test/test_edr.py index 3f3fe888..facd1baa 100644 --- a/api/test/test_edr.py +++ b/api/test/test_edr.py @@ -118,12 +118,16 @@ def test_get_area_with_incorrect_coords(): response = client.get("/collections/observations/area?coords=POLYGON((22.12 59.86, 24.39 60.41))") assert response.status_code == 422 + assert response.json() == { + "detail": {"coords": "Invalid or unparseable wkt provided: POLYGON((22.12 59.86, 24.39 60.41))"} + } def test_get_area_with_incorrect_geometry_type(): response = client.get("/collections/observations/area?coords=POINT(22.12 59.86)") assert response.status_code == 422 + assert response.json() == {"detail": {"coords": "Invalid geometric type: Point"}} def test_get_position_with_normal_query(): @@ -158,7 +162,7 @@ def test_get_position_with_incorrect_coords(): response = client.get("/collections/observations/position?coords=POINT(60.41)") assert response.status_code == 422 - assert response.json() == {"detail": {"coords": "Invalid coordinates: POINT(60.41)"}} + assert response.json() == {"detail": {"coords": "Invalid or unparseable wkt provided: POINT(60.41)"}} def test_get_position_with_incorrect_geometry_type(): @@ -168,11 +172,7 @@ def test_get_position_with_incorrect_geometry_type(): ) assert response.status_code == 422 - assert response.json() == { - "detail": { - "coords": "Invalid coordinates: POLYGON((22.12 59.86, 24.39 60.41, 24.39 60.41, 24.39 59.86, 22.12 59.86))" - } - } + assert response.json() == {"detail": {"coords": "Invalid geometric type: Polygon"}} def create_mock_obs_response(json_data):