From bcff3a0cc00a517c37ac8ae574d9df382d81da82 Mon Sep 17 00:00:00 2001 From: emileten Date: Thu, 6 Apr 2023 11:09:11 +0900 Subject: [PATCH 1/3] exception handler in ingestion api --- lib/ingestor-api/runtime/src/main.py | 7 +++++++ lib/ingestor-api/runtime/tests/test_registration.py | 7 +------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/ingestor-api/runtime/src/main.py b/lib/ingestor-api/runtime/src/main.py index 8ab4678..4fe602f 100644 --- a/lib/ingestor-api/runtime/src/main.py +++ b/lib/ingestor-api/runtime/src/main.py @@ -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 @@ -123,3 +125,8 @@ 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) \ No newline at end of file diff --git a/lib/ingestor-api/runtime/tests/test_registration.py b/lib/ingestor-api/runtime/tests/test_registration.py index 55721de..4f1a634 100644 --- a/lib/ingestor-api/runtime/tests/test_registration.py +++ b/lib/ingestor-api/runtime/tests/test_registration.py @@ -119,12 +119,7 @@ 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"] - ] - ), "should reference asset type in validation error response" + assert asset_type in response.json(), "should reference asset type in validation error response" assert ( len(self.db.fetch_many(status="queued")["items"]) == 0 ), "data should not be stored in DB" From 0630b35d115c4ea8688af95e479dfd14d12fb5ca Mon Sep 17 00:00:00 2001 From: emileten Date: Thu, 6 Apr 2023 11:09:54 +0900 Subject: [PATCH 2/3] make item_assets optional --- lib/ingestor-api/runtime/src/schemas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ingestor-api/runtime/src/schemas.py b/lib/ingestor-api/runtime/src/schemas.py index 79db560..021ab53 100644 --- a/lib/ingestor-api/runtime/src/schemas.py +++ b/lib/ingestor-api/runtime/src/schemas.py @@ -52,7 +52,7 @@ def exists(cls, collection): class StacCollection(Collection): id: str - item_assets: Dict + item_assets: Optional[Dict] class Status(str, enum.Enum): From 0beb57990f01cca0d2a29a4d73eeacb77237ba96 Mon Sep 17 00:00:00 2001 From: emileten Date: Thu, 6 Apr 2023 11:17:27 +0900 Subject: [PATCH 3/3] format --- lib/ingestor-api/runtime/src/main.py | 3 ++- lib/ingestor-api/runtime/tests/conftest.py | 2 +- lib/ingestor-api/runtime/tests/test_registration.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/ingestor-api/runtime/src/main.py b/lib/ingestor-api/runtime/src/main.py index 4fe602f..e3696ad 100644 --- a/lib/ingestor-api/runtime/src/main.py +++ b/lib/ingestor-api/runtime/src/main.py @@ -126,7 +126,8 @@ def who_am_i(username=Depends(dependencies.get_username)): """ return {"username": username} + # exception handling @app.exception_handler(RequestValidationError) async def validation_exception_handler(request, exc): - return JSONResponse(str(exc), status_code=422) \ No newline at end of file + return JSONResponse(str(exc), status_code=422) diff --git a/lib/ingestor-api/runtime/tests/conftest.py b/lib/ingestor-api/runtime/tests/conftest.py index f2d357e..ae03df7 100644 --- a/lib/ingestor-api/runtime/tests/conftest.py +++ b/lib/ingestor-api/runtime/tests/conftest.py @@ -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) diff --git a/lib/ingestor-api/runtime/tests/test_registration.py b/lib/ingestor-api/runtime/tests/test_registration.py index 4f1a634..0133759 100644 --- a/lib/ingestor-api/runtime/tests/test_registration.py +++ b/lib/ingestor-api/runtime/tests/test_registration.py @@ -119,7 +119,9 @@ 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 asset_type in response.json(), "should reference asset type in validation error response" + assert ( + asset_type in response.json() + ), "should reference asset type in validation error response" assert ( len(self.db.fetch_many(status="queued")["items"]) == 0 ), "data should not be stored in DB"