Skip to content

Commit 1d6cc9e

Browse files
committed
return validation errors as JSON so that technical users can see where they fked up
1 parent d4cd5d3 commit 1d6cc9e

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

masterbase/app.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import requests
99
import uvicorn
1010
from litestar import Litestar, MediaType, Request, WebSocket, get, post
11-
from litestar.exceptions import HTTPException, PermissionDeniedException
11+
from litestar.exceptions import HTTPException, PermissionDeniedException, ValidationException
1212
from litestar.handlers import WebsocketListener
1313
from litestar.response import Redirect, Response, Stream
1414
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
@@ -172,12 +172,12 @@ async def demodata(request: Request, api_key: str, session_id: str) -> Stream:
172172

173173

174174
@get("/db_export", guards=[valid_key_guard, analyst_guard], sync_to_thread=False)
175-
def db_export(request: Request, api_key: str, table: ExportTable) -> Stream:
175+
def db_export(request: Request, api_key: str, table: ExportTable, since: datetime | None = None) -> Stream:
176176
"""Return a database export of the requested `table`."""
177177
engine = request.app.state.engine
178178
filename = f"{table.value}-{datetime.now()}.csv"
179179
return Stream(
180-
lambda: db_export_chunks(engine, table.value),
180+
lambda: db_export_chunks(engine, table.value, since),
181181
headers={
182182
"Content-Type": "text/csv",
183183
"Content-Disposition": f"attachment; filename={filename}",
@@ -378,9 +378,11 @@ def provision_handler(request: Request) -> str:
378378
"""
379379

380380

381-
def plain_text_exception_handler(_: Request, exception: Exception) -> Response:
381+
def readable_exception_handler(_: Request, exception: Exception) -> Response:
382382
"""Handle exceptions subclassed from HTTPException."""
383383
status_code = getattr(exception, "status_code", HTTP_500_INTERNAL_SERVER_ERROR)
384+
if isinstance(exception, ValidationException):
385+
return Response(json={"detail": "Validation error!", "errors": exception.extra}, status_code=status_code)
384386
if isinstance(exception, HTTPException):
385387
content = exception.detail
386388
else:
@@ -409,7 +411,7 @@ def plain_text_exception_handler(_: Request, exception: Exception) -> Response:
409411
report_player,
410412
db_export,
411413
],
412-
exception_handlers={Exception: plain_text_exception_handler},
414+
exception_handlers={Exception: readable_exception_handler},
413415
on_shutdown=shutdown_registers,
414416
opt={"DEVELOPMENT": bool(os.getenv("DEVELOPMENT"))},
415417
)

0 commit comments

Comments
 (0)