diff --git a/tests/unit/test_reconcile.py b/tests/unit/test_reconcile.py index c7443cce..1a93e726 100644 --- a/tests/unit/test_reconcile.py +++ b/tests/unit/test_reconcile.py @@ -23,6 +23,12 @@ def test_reconcile_post(): assert res[0]["id"] == "Q18634850", res +def test_reconcile_invalid(): + queries = {"mutti": {"query": 37473874}} + resp = client.post("/reconcile/default", data={"queries": json.dumps(queries)}) + assert resp.status_code == 400, resp.text + + def test_reconcile_suggest_entity_no_prefix(): resp = client.get("/reconcile/default/suggest/entity") assert resp.status_code == 200, resp.text @@ -37,8 +43,9 @@ def test_reconcile_suggest_entity_prefix(): res = resp.json()["result"] assert len(res) > 0, res assert "Q7747" == res[0]["id"], res - name = ascii_text(res[0]["name"]).lower() - assert "vladimir" in name, name + name = ascii_text(res[0]["name"]) + assert name is not None, res + assert "vladimir" in name.lower(), name def test_reconcile_suggest_entity_prefix_dummy(): diff --git a/yente/app.py b/yente/app.py index 033408ba..e896fcfc 100644 --- a/yente/app.py +++ b/yente/app.py @@ -4,6 +4,7 @@ from typing import AsyncGenerator, Dict, Type, Callable, Any, Coroutine, Union from contextlib import asynccontextmanager from elasticsearch import ApiError, TransportError +from pydantic import ValidationError from fastapi import FastAPI from fastapi import Request, Response from fastapi.middleware.cors import CORSMiddleware @@ -87,9 +88,16 @@ async def transport_error_handler(request: Request, exc: TransportError) -> Resp return JSONResponse(status_code=500, content={"detail": exc.message}) +async def validation_error_handler(request: Request, exc: ValidationError) -> Response: + log.warn(f"Validation error: {exc}") + body = {"detail": exc.title, "errors": exc.errors()} + return JSONResponse(status_code=400, content=body) + + HANDLERS: Dict[Union[Type[Exception], int], ExceptionHandler] = { ApiError: api_error_handler, TransportError: transport_error_handler, + ValidationError: validation_error_handler, }