Skip to content

improve exception and message for bbox parsing #835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion stac_fastapi/api/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 11 additions & 3 deletions stac_fastapi/types/stac_fastapi/types/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down