Skip to content

Commit

Permalink
fix exception handling type FUBAR
Browse files Browse the repository at this point in the history
  • Loading branch information
pudo committed Jan 2, 2024
1 parent 7ed4006 commit 7094cc3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions yente/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
import aiocron # type: ignore
from uuid import uuid4
from typing import AsyncGenerator
from typing import AsyncGenerator, Dict, Type, Callable, Any, Coroutine, Union
from contextlib import asynccontextmanager
from elasticsearch import ApiError, TransportError
from fastapi import FastAPI
Expand All @@ -19,6 +19,7 @@
from yente.search.indexer import update_index_threaded

log = get_logger("yente")
ExceptionHandler = Callable[[Request, Any], Coroutine[Any, Any, Response]]


async def cron_task() -> None:
Expand Down Expand Up @@ -85,13 +86,20 @@ async def transport_error_handler(request: Request, exc: TransportError) -> Resp
return JSONResponse(status_code=500, content={"detail": exc.message})


HANDLERS: Dict[Union[Type[Exception], int], ExceptionHandler] = {
ApiError: api_error_handler,
TransportError: transport_error_handler,
}


def create_app() -> FastAPI:
app = FastAPI(
title=settings.TITLE,
description=settings.DESCRIPTION,
version=settings.VERSION,
contact=settings.CONTACT,
openapi_tags=settings.TAGS,
exception_handlers=HANDLERS,
redoc_url="/",
lifespan=lifespan,
)
Expand All @@ -107,7 +115,4 @@ def create_app() -> FastAPI:
app.include_router(search.router)
app.include_router(reconcile.router)
app.include_router(admin.router)

app.add_exception_handler(ApiError, api_error_handler)
app.add_exception_handler(TransportError, transport_error_handler)
return app

0 comments on commit 7094cc3

Please sign in to comment.