Skip to content

Commit

Permalink
Merge pull request #433 from opensanctions/pudo/handle-validation-error
Browse files Browse the repository at this point in the history
Handle validation error
  • Loading branch information
pudo authored May 8, 2024
2 parents fb45b0a + a718726 commit 72c6351
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions tests/unit/test_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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():
Expand Down
8 changes: 8 additions & 0 deletions yente/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}


Expand Down

0 comments on commit 72c6351

Please sign in to comment.