Skip to content

Commit

Permalink
Fix: Return 400 instead of 422 as per EDR specification
Browse files Browse the repository at this point in the history
  • Loading branch information
fjugipe committed Feb 9, 2024
1 parent f1b1986 commit f700ccb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ def get_datetime_range(datetime_string: str | None) -> Tuple[Timestamp, Timestam
errors["datetime"] = f"Invalid format: {datetime_string}"

if errors:
raise HTTPException(status_code=422, detail=errors)
raise HTTPException(status_code=400, detail=errors)

return start_datetime, end_datetime
12 changes: 6 additions & 6 deletions api/routers/edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ async def get_data_position(
raise TypeError
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}"})
raise HTTPException(status_code=400, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"})
except TypeError:
raise HTTPException(status_code=422, detail={"coords": f"Invalid geometric type: {point.geom_type}"})
raise HTTPException(status_code=400, detail={"coords": f"Invalid geometric type: {point.geom_type}"})
except Exception:
raise HTTPException(
status_code=422, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"}
status_code=400, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"}
)

return await get_data_area(poly.wkt, parameter_name, datetime, f)
Expand All @@ -133,12 +133,12 @@ async def get_data_area(
if poly.geom_type != "Polygon":
raise TypeError
except GEOSException:
raise HTTPException(status_code=422, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"})
raise HTTPException(status_code=400, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"})
except TypeError:
raise HTTPException(status_code=422, detail={"coords": f"Invalid geometric type: {poly.geom_type}"})
raise HTTPException(status_code=400, detail={"coords": f"Invalid geometric type: {poly.geom_type}"})
except Exception:
raise HTTPException(
status_code=422, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"}
status_code=400, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"}
)

range = get_datetime_range(datetime)
Expand Down
12 changes: 6 additions & 6 deletions api/test/test_edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_get_locations_id_without_parameter_names_query():
def test_get_locations_id_with_incorrect_datetime_format():
response = client.get("/collections/observations/locations/06260?datetime=20221231T000000Z/20221231T010000Z")

assert response.status_code == 422
assert response.status_code == 400
assert response.json() == {"detail": {"datetime": "Invalid format: 20221231T000000Z/20221231T010000Z"}}


Expand All @@ -67,7 +67,7 @@ def test_get_locations_id_with_incorrect_datetime_range():
"/collections/observations/locations/06260?datetime=2024-12-31T00:00:00Z/2022-12-31T01:00:00Z"
)

assert response.status_code == 422
assert response.status_code == 400
assert response.json() == {"detail": {"datetime": "Invalid range: 2024-12-31T00:00:00Z > 2022-12-31T01:00:00Z"}}


Expand Down Expand Up @@ -113,7 +113,7 @@ def test_get_area_with_normal_query():
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.status_code == 400
assert response.json() == {
"detail": {"coords": "Invalid or unparseable wkt provided: POLYGON((22.12 59.86, 24.39 60.41))"}
}
Expand All @@ -122,7 +122,7 @@ def test_get_area_with_incorrect_coords():
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.status_code == 400
assert response.json() == {"detail": {"coords": "Invalid geometric type: Point"}}


Expand Down Expand Up @@ -157,7 +157,7 @@ def test_get_position_with_normal_query():
def test_get_position_with_incorrect_coords():
response = client.get("/collections/observations/position?coords=POINT(60.41)")

assert response.status_code == 422
assert response.status_code == 400
assert response.json() == {"detail": {"coords": "Invalid or unparseable wkt provided: POINT(60.41)"}}


Expand All @@ -167,5 +167,5 @@ def test_get_position_with_incorrect_geometry_type():
"24.39 60.41, 24.39 59.86, 22.12 59.86))"
)

assert response.status_code == 422
assert response.status_code == 400
assert response.json() == {"detail": {"coords": "Invalid geometric type: Polygon"}}

0 comments on commit f700ccb

Please sign in to comment.