Skip to content

Commit fd90587

Browse files
committed
improve exception and message for bbox parsing
1 parent c4e6ead commit fd90587

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- avoid future deprecation for pydantic.Field and use `json_schema_extra` instead of `openapi_examples`
88
- use `orjson` based JSONResponse when available
9+
- changed from `AssertionError` to `HTTPException` for **bbox** parsing exceptions
910

1011
## [5.2.0] - 2025-04-18
1112

stac_fastapi/api/tests/test_models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ def test_create_get_request_model():
4343
model = request_model(bbox="0,0,0,1,1,1")
4444
assert model.bbox == (0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
4545

46-
with pytest.raises(AssertionError):
46+
with pytest.raises(HTTPException):
47+
request_model(bbox="a,b")
48+
49+
with pytest.raises(HTTPException):
4750
request_model(bbox="0,0,0,1,1")
4851

4952
model = request_model(

stac_fastapi/types/stac_fastapi/types/search.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Dict, List, Optional, Union
66

77
import attr
8-
from fastapi import Query
8+
from fastapi import HTTPException, Query
99
from pydantic import Field, PositiveInt
1010
from pydantic.functional_validators import AfterValidator
1111
from stac_pydantic.api import Search
@@ -34,8 +34,16 @@ def str2list(x: str) -> Optional[List[str]]:
3434
def str2bbox(x: str) -> Optional[BBox]:
3535
"""Convert string to BBox based on , delimiter."""
3636
if x:
37-
t = tuple(float(v) for v in x.split(","))
38-
assert len(t) in [4, 6], f"BBox '{x}' must have 4 or 6 values."
37+
try:
38+
t = tuple(float(v) for v in x.split(","))
39+
except ValueError:
40+
raise HTTPException(status_code=400, detail=f"invalid bbox: {x}")
41+
42+
if len(t) not in (4, 6):
43+
raise HTTPException(
44+
status_code=400, detail=f"BBox '{x}' must have 4 or 6 values."
45+
)
46+
3947
return t
4048

4149
return None

0 commit comments

Comments
 (0)