|
10 | 10 |
|
11 | 11 | from backend import KernelBackend |
12 | 12 | from consts import SubmissionMode |
13 | | -from fastapi import Depends, FastAPI, Header, HTTPException, UploadFile |
14 | | -from fastapi.responses import StreamingResponse |
| 13 | +from fastapi import Depends, FastAPI, Header, HTTPException, Request, UploadFile |
| 14 | +from fastapi.responses import JSONResponse, StreamingResponse |
15 | 15 | from leaderboard_db import LeaderboardRankedEntry |
16 | 16 | from submission import SubmissionRequest |
| 17 | +from utils import KernelBotError |
17 | 18 |
|
18 | | -from .utils import _handle_discord_oauth, _handle_github_oauth, _run_submission |
| 19 | +from .api_utils import _handle_discord_oauth, _handle_github_oauth, _run_submission |
19 | 20 |
|
20 | 21 | # yes, we do want ... = Depends() in function signatures |
21 | 22 | # ruff: noqa: B008 |
@@ -60,18 +61,18 @@ def init_api(_backend_instance: KernelBackend): |
60 | 61 | backend_instance = _backend_instance |
61 | 62 |
|
62 | 63 |
|
| 64 | +@app.exception_handler(KernelBotError) |
| 65 | +async def kernel_bot_error_handler(req: Request, exc: KernelBotError): |
| 66 | + return JSONResponse(status_code=exc.http_code, content={"message": str(exc)}) |
| 67 | + |
| 68 | + |
63 | 69 | @contextmanager |
64 | 70 | def get_db(): |
65 | 71 | """Database context manager with guaranteed error handling""" |
66 | 72 | if not backend_instance: |
67 | 73 | raise HTTPException(status_code=500, detail="Bot instance not initialized") |
68 | 74 |
|
69 | | - if not hasattr(backend_instance, "leaderboard_db"): |
70 | | - raise HTTPException(status_code=500, detail="Database not initialized") |
71 | | - |
72 | 75 | with backend_instance.db as db: |
73 | | - if db is None: |
74 | | - raise HTTPException(status_code=500, detail="Database connection failed") |
75 | 76 | yield db |
76 | 77 |
|
77 | 78 |
|
|
0 commit comments