|
8 | 8 | import requests
|
9 | 9 | import uvicorn
|
10 | 10 | from litestar import Litestar, MediaType, Request, WebSocket, get, post
|
11 |
| -from litestar.exceptions import HTTPException, PermissionDeniedException |
| 11 | +from litestar.exceptions import HTTPException, PermissionDeniedException, ValidationException |
12 | 12 | from litestar.handlers import WebsocketListener
|
13 | 13 | from litestar.response import Redirect, Response, Stream
|
14 | 14 | 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:
|
172 | 172 |
|
173 | 173 |
|
174 | 174 | @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: |
176 | 176 | """Return a database export of the requested `table`."""
|
177 | 177 | engine = request.app.state.engine
|
178 | 178 | filename = f"{table.value}-{datetime.now()}.csv"
|
179 | 179 | return Stream(
|
180 |
| - lambda: db_export_chunks(engine, table.value), |
| 180 | + lambda: db_export_chunks(engine, table.value, since), |
181 | 181 | headers={
|
182 | 182 | "Content-Type": "text/csv",
|
183 | 183 | "Content-Disposition": f"attachment; filename={filename}",
|
@@ -378,9 +378,11 @@ def provision_handler(request: Request) -> str:
|
378 | 378 | """
|
379 | 379 |
|
380 | 380 |
|
381 |
| -def plain_text_exception_handler(_: Request, exception: Exception) -> Response: |
| 381 | +def readable_exception_handler(_: Request, exception: Exception) -> Response: |
382 | 382 | """Handle exceptions subclassed from HTTPException."""
|
383 | 383 | 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) |
384 | 386 | if isinstance(exception, HTTPException):
|
385 | 387 | content = exception.detail
|
386 | 388 | else:
|
@@ -409,7 +411,7 @@ def plain_text_exception_handler(_: Request, exception: Exception) -> Response:
|
409 | 411 | report_player,
|
410 | 412 | db_export,
|
411 | 413 | ],
|
412 |
| - exception_handlers={Exception: plain_text_exception_handler}, |
| 414 | + exception_handlers={Exception: readable_exception_handler}, |
413 | 415 | on_shutdown=shutdown_registers,
|
414 | 416 | opt={"DEVELOPMENT": bool(os.getenv("DEVELOPMENT"))},
|
415 | 417 | )
|
|
0 commit comments