Skip to content

Commit da1cef7

Browse files
committed
db __enter__ *always* either returns the db or throws, we no longer need to check for None
1 parent fef80aa commit da1cef7

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

File renamed without changes.

src/discord-cluster-manager/api/main.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
from backend import KernelBackend
1212
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
1515
from leaderboard_db import LeaderboardRankedEntry
1616
from submission import SubmissionRequest
17+
from utils import KernelBotError
1718

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
1920

2021
# yes, we do want ... = Depends() in function signatures
2122
# ruff: noqa: B008
@@ -60,18 +61,18 @@ def init_api(_backend_instance: KernelBackend):
6061
backend_instance = _backend_instance
6162

6263

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+
6369
@contextmanager
6470
def get_db():
6571
"""Database context manager with guaranteed error handling"""
6672
if not backend_instance:
6773
raise HTTPException(status_code=500, detail="Bot instance not initialized")
6874

69-
if not hasattr(backend_instance, "leaderboard_db"):
70-
raise HTTPException(status_code=500, detail="Database not initialized")
71-
7275
with backend_instance.db as db:
73-
if db is None:
74-
raise HTTPException(status_code=500, detail="Database connection failed")
7576
yield db
7677

7778

src/discord-cluster-manager/leaderboard_db.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def disconnect(self):
5757
self.cursor = None
5858
self.connection = None
5959

60-
def __enter__(self):
60+
def __enter__(self) -> "LeaderboardDB":
6161
"""Context manager entry"""
6262
if self.connection is not None:
6363
self.refcount += 1
@@ -66,7 +66,8 @@ def __enter__(self):
6666
if self.connect():
6767
self.refcount = 1
6868
return self
69-
return None
69+
70+
raise KernelBotError("Could not connect to database", code=500)
7071

7172
def __exit__(self, exc_type, exc_val, exc_tb):
7273
"""Context manager exit"""

src/discord-cluster-manager/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class KernelBotError(Exception):
3030
risk of leaking internal bot details.
3131
"""
3232

33-
def __init__(self, message):
33+
def __init__(self, message, code: int = 400):
3434
super().__init__(message)
35+
self.http_code = 400
3536

3637

3738
def get_github_branch_name():

0 commit comments

Comments
 (0)