Skip to content

Commit

Permalink
feat!: Remove deprecated litestar.middleware.exceptions module and …
Browse files Browse the repository at this point in the history
…deprecated params of internal `ExceptionHandlerMiddleware` (#3435)

* refactor!: Remove deprecated `app` param of `Response.to_asgi_response` (#3393)
* Remove 'app' parameter from `.to_asgi_response`
* Remove debug param
* Remove exception_handlers param
* Remove litestar.middleware.exceptions
  • Loading branch information
provinzkraut committed Aug 27, 2024
1 parent 178eb92 commit 37895ee
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 123 deletions.
16 changes: 16 additions & 0 deletions docs/release-notes/whats-new-3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,19 @@ After:
@get(handler_class=MyHTTPRouteHandler)
async def handler() -> Any:
...
Deprecated ``app`` parameter for ``Response.to_asgi_response`` has been removed
-------------------------------------------------------------------------------

The parameter ``app`` for :meth:`~response.Response.to_asgi_response` has been removed.
If you need access to the app instance inside a custom ``to_asgi_response`` method,
replace the usages of ``app`` with ``request.app``.


Removal of deprecated ``litestar.middleware.exceptions`` module and ``ExceptionHandlerMiddleware``
--------------------------------------------------------------------------------------------------

The deprecated ``litestar.middleware.exceptions`` module and the
``ExceptionHandlerMiddleware`` have been removed. Since ``ExceptionHandlerMiddleware``
has been applied automatically behind the scenes if necessary, no action is required.
2 changes: 1 addition & 1 deletion litestar/_asgi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def wrap_in_exception_handler(app: ASGIApp) -> ASGIApp:
"""
from litestar.middleware._internal.exceptions import ExceptionHandlerMiddleware

return ExceptionHandlerMiddleware(app=app, debug=None)
return ExceptionHandlerMiddleware(app=app)


def get_route_handlers(route: BaseRoute) -> list[RouteHandlerType]:
Expand Down
41 changes: 2 additions & 39 deletions litestar/middleware/_internal/exceptions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
create_debug_response,
)
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
from litestar.utils.deprecation import warn_deprecation
from litestar.utils.empty import value_or_raise
from litestar.utils.scope.state import ScopeState

Expand Down Expand Up @@ -92,46 +91,14 @@ class ExceptionHandlerMiddleware:
This used in multiple layers of Litestar.
"""

def __init__(
self, app: ASGIApp, debug: bool | None, exception_handlers: ExceptionHandlersMap | None = None
) -> None:
def __init__(self, app: ASGIApp) -> None:
"""Initialize ``ExceptionHandlerMiddleware``.
Args:
app: The ``next`` ASGI app to call.
debug: Whether ``debug`` mode is enabled. Deprecated. Debug mode will be inferred from the request scope
exception_handlers: A dictionary mapping status codes and/or exception types to handler functions.
.. deprecated:: 2.0.0
The ``debug`` parameter is deprecated. It will be inferred from the request scope
.. deprecated:: 2.9.0
The ``exception_handlers`` parameter is deprecated. It will be inferred from the application or the
route handler.
"""
self.app = app
self.exception_handlers = exception_handlers
self.debug = debug

if debug is not None:
warn_deprecation(
"2.0.0",
deprecated_name="debug",
kind="parameter",
info="Debug mode will be inferred from the request scope",
removal_in="3.0.0",
)

if exception_handlers is not None:
warn_deprecation(
"2.9.0",
deprecated_name="exception_handlers",
kind="parameter",
info="It will be inferred from the application or the route handler",
removal_in="3.0.0",
)

self._get_debug = self._get_debug_scope if debug is None else lambda *a: debug

@staticmethod
def _get_debug_scope(scope: Scope) -> bool:
Expand Down Expand Up @@ -195,11 +162,7 @@ async def handle_request_exception(
None.
"""

exception_handlers = (
value_or_raise(ScopeState.from_scope(scope).exception_handlers)
if self.exception_handlers is None
else self.exception_handlers
)
exception_handlers = value_or_raise(ScopeState.from_scope(scope).exception_handlers)
exception_handler = get_exception_handler(exception_handlers, exc) or self.default_http_exception_handler
request: Request[Any, Any, Any] = litestar_app.request_class(scope=scope, receive=receive, send=send)
response = exception_handler(request, exc)
Expand Down
19 changes: 0 additions & 19 deletions litestar/middleware/exceptions/__init__.py

This file was deleted.

20 changes: 0 additions & 20 deletions litestar/middleware/exceptions/_debug_response.py

This file was deleted.

41 changes: 0 additions & 41 deletions litestar/middleware/exceptions/middleware.py

This file was deleted.

2 changes: 1 addition & 1 deletion litestar/security/session_auth/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
scopes=self.config.scopes,
retrieve_user_handler=self.config.retrieve_user_handler, # type: ignore[arg-type]
)
exception_middleware = ExceptionHandlerMiddleware(app=auth_middleware, debug=None)
exception_middleware = ExceptionHandlerMiddleware(app=auth_middleware)
self.app = self.config.session_backend_config.middleware.middleware(
app=exception_middleware,
backend=self.config.session_backend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def app() -> Litestar:

@pytest.fixture()
def middleware() -> ExceptionHandlerMiddleware:
return ExceptionHandlerMiddleware(dummy_app, None)
return ExceptionHandlerMiddleware(dummy_app)


@pytest.fixture()
Expand Down Expand Up @@ -416,7 +416,7 @@ async def asgi_app(scope: Scope, receive: Receive, send: Send) -> None:
await send(start_message)
raise RuntimeError("Test exception")

mw = ExceptionHandlerMiddleware(asgi_app, None)
mw = ExceptionHandlerMiddleware(asgi_app)

with pytest.raises(LitestarException):
await mw(scope, mock_receive, mock_send)
Expand Down

0 comments on commit 37895ee

Please sign in to comment.