Skip to content
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

feat(ingestor): validation exception handler #35

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions lib/ingestor-api/runtime/src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from fastapi import Depends, FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from . import collection as collection_loader
from . import config, dependencies, schemas, services
Expand Down Expand Up @@ -123,3 +125,9 @@ def who_am_i(username=Depends(dependencies.get_username)):
Return username for the provided request
"""
return {"username": username}


# exception handling
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
return JSONResponse(str(exc), status_code=422)
emileten marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion lib/ingestor-api/runtime/src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def exists(cls, collection):

class StacCollection(Collection):
id: str
item_assets: Dict
item_assets: Optional[Dict]
emileten marked this conversation as resolved.
Show resolved Hide resolved


class Status(str, enum.Enum):
Expand Down
2 changes: 1 addition & 1 deletion lib/ingestor-api/runtime/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def client_authenticated(app):
"""
from src.dependencies import get_username

app.dependency_overrides[get_username] = lambda: 'test_user'
app.dependency_overrides[get_username] = lambda: "test_user"
return TestClient(app)


Expand Down
7 changes: 2 additions & 5 deletions lib/ingestor-api/runtime/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,8 @@ def test_validates_missing_assets(
)
assert response.status_code == 422, "should get validation error"
for asset_type in self.example_ingestion.item.assets.keys():
assert any(
[
err["loc"] == ["body", "assets", asset_type, "href"]
for err in response.json()["detail"]
]
assert (
asset_type in response.json()
), "should reference asset type in validation error response"
assert (
len(self.db.fetch_many(status="queued")["items"]) == 0
Expand Down