Skip to content

Commit

Permalink
Merge pull request #7 from peterschutt/catch-mounted-exceptions
Browse files Browse the repository at this point in the history
fix: catch errors from mounted app
  • Loading branch information
peterschutt authored May 6, 2024
2 parents 2c2a14f + 1fd147b commit b6ab7df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/sqladmin_litestar_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Any

import sqladmin
Expand All @@ -25,6 +26,8 @@

__all__ = ("SQLAdminPlugin",)

logger = logging.getLogger(__name__)


class SQLAdminPlugin(InitPluginProtocol):
def __init__( # noqa: PLR0913
Expand Down Expand Up @@ -90,8 +93,9 @@ async def wrapped_app(scope: Scope, receive: Receive, send: Send) -> None:
app = scope["app"]
try:
await self.app(scope, receive, send) # type: ignore[arg-type]
finally:
scope["app"] = app
except Exception:
logger.exception("Error raised from SQLAdmin app")
scope["app"] = app

app_config.route_handlers.append(wrapped_app)
return app_config
Expand Down
7 changes: 4 additions & 3 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ def test_views_added_to_admin_app(plugin: SQLAdminPlugin, monkeypatch: pytest.Mo
mock.assert_called_once_with(plugin.views[0])


@pytest.mark.parametrize("should_raise", [True, False])
@pytest.mark.anyio()
async def test_resets_app_in_scope(
plugin: SQLAdminPlugin, app: Litestar, monkeypatch: pytest.MonkeyPatch
*, should_raise: bool, plugin: SQLAdminPlugin, app: Litestar, monkeypatch: pytest.MonkeyPatch
) -> None:
mock = MagicMock()
mock = MagicMock(side_effect=RuntimeError if should_raise else None)

async def fake_admin_app(scope: Scope, _: Receive, __: Send) -> None: # noqa: RUF029
mock()
scope["app"] = "admin" # type: ignore[arg-type]
mock()

monkeypatch.setattr(plugin, "app", fake_admin_app)
handler = app.route_handler_method_map["/"]["asgi"].fn
Expand Down

0 comments on commit b6ab7df

Please sign in to comment.