diff --git a/CHANGES.md b/CHANGES.md index 36c193fb..3966472a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,7 @@ - avoid future deprecation for pydantic.Field and use `json_schema_extra` instead of `openapi_examples` - use `orjson` based JSONResponse when available +- changed from `AssertionError` to `HTTPException` for **bbox** parsing exceptions ## [5.2.0] - 2025-04-18 diff --git a/stac_fastapi/api/tests/test_models.py b/stac_fastapi/api/tests/test_models.py index 032b6002..2f77712d 100644 --- a/stac_fastapi/api/tests/test_models.py +++ b/stac_fastapi/api/tests/test_models.py @@ -43,7 +43,10 @@ def test_create_get_request_model(): model = request_model(bbox="0,0,0,1,1,1") assert model.bbox == (0.0, 0.0, 0.0, 1.0, 1.0, 1.0) - with pytest.raises(AssertionError): + with pytest.raises(HTTPException): + request_model(bbox="a,b") + + with pytest.raises(HTTPException): request_model(bbox="0,0,0,1,1") model = request_model( diff --git a/stac_fastapi/types/stac_fastapi/types/search.py b/stac_fastapi/types/stac_fastapi/types/search.py index f52f4530..01e62273 100644 --- a/stac_fastapi/types/stac_fastapi/types/search.py +++ b/stac_fastapi/types/stac_fastapi/types/search.py @@ -5,7 +5,7 @@ from typing import Dict, List, Optional, Union import attr -from fastapi import Query +from fastapi import HTTPException, Query from pydantic import Field, PositiveInt from pydantic.functional_validators import AfterValidator from stac_pydantic.api import Search @@ -34,8 +34,16 @@ def str2list(x: str) -> Optional[List[str]]: def str2bbox(x: str) -> Optional[BBox]: """Convert string to BBox based on , delimiter.""" if x: - t = tuple(float(v) for v in x.split(",")) - assert len(t) in [4, 6], f"BBox '{x}' must have 4 or 6 values." + try: + t = tuple(float(v) for v in x.split(",")) + except ValueError: + raise HTTPException(status_code=400, detail=f"invalid bbox: {x}") + + if len(t) not in (4, 6): + raise HTTPException( + status_code=400, detail=f"BBox '{x}' must have 4 or 6 values." + ) + return t return None