Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔨 Add http_exception handler to Admin class #694

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions sqladmin/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
List,
Optional,
Expand Down Expand Up @@ -371,15 +372,17 @@ def __init__(

statics = StaticFiles(packages=["sqladmin"])

# def http_exception(request: Request, exc: Exception) -> Response:
# assert isinstance(exc, HTTPException)
# context = {
# "status_code": exc.status_code,
# "message": exc.detail,
# }
# return self.templates.TemplateResponse(
# request, "error.html", context, status_code=exc.status_code
# )
async def http_exception(
request: Request, exc: Exception
) -> Union[Response, Awaitable[Response]]:
assert isinstance(exc, HTTPException)
context = {
"status_code": exc.status_code,
"message": exc.detail,
}
return await self.templates.TemplateResponse(
request, "error.html", context, status_code=exc.status_code
)

routes = [
Mount("/statics", app=statics, name="statics"),
Expand Down Expand Up @@ -417,7 +420,7 @@ def __init__(
]

self.admin.router.routes = routes
# self.admin.exception_handlers = {HTTPException: http_exception}
self.admin.exception_handlers = {HTTPException: http_exception}
self.admin.debug = debug
self.app.mount(base_url, app=self.admin, name="admin")

Expand Down
2 changes: 1 addition & 1 deletion sqladmin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def write(self, value: T) -> T:


def stream_to_csv(
callback: Callable[[Writer], AsyncGenerator[T, None]]
callback: Callable[[Writer], AsyncGenerator[T, None]],
) -> Generator[T, None, None]:
"""Function that takes a callable (that yields from a CSV Writer), and
provides it a writer that streams the output directly instead of
Expand Down
Loading