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

Generic ExceptionHandler type #2403

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d9037c5
generic exception
tekumara Jan 8, 2024
7cecce7
ruff format
tekumara Jan 8, 2024
f35c796
bind exc_class_or_status_code to E
tekumara Jan 8, 2024
2300839
Revert "ruff format"
tekumara Jan 8, 2024
6cf35d6
ruff format starlette/types.py
tekumara Jan 8, 2024
ba30a49
s/E/ExceptionType/
tekumara Jan 8, 2024
e94fc1c
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara Jan 8, 2024
77cca8b
contravariance is redundant because Callable is contravariant
tekumara Jan 9, 2024
1aebedb
Merge branch 'master' into exception-handler-typing
tekumara Jan 9, 2024
da3f8bb
Merge branch 'master' into exception-handler-typing
tekumara Jan 12, 2024
145a9b1
Merge branch 'master' into exception-handler-typing
tekumara Jan 17, 2024
c71d90c
Merge branch 'master' into exception-handler-typing
Kludex Feb 3, 2024
64328f4
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara Mar 11, 2024
6f89af7
Merge branch 'master' into exception-handler-typing
tekumara Mar 29, 2024
1890a85
Merge branch 'master' into exception-handler-typing
tekumara May 11, 2024
adfafdf
Merge branch 'master' into exception-handler-typing
tekumara Jul 20, 2024
ac72313
Merge branch 'master' into exception-handler-typing
tekumara Aug 6, 2024
25fafd2
Merge branch 'master' into exception-handler-typing
tekumara Aug 17, 2024
c0cbf4a
Merge branch 'master' of https://github.com/tekumara/starlette into e…
tekumara Sep 2, 2024
bd81d9f
ruff format
tekumara Sep 2, 2024
292259c
overload __init__ to support multiple exception handlers
tekumara Sep 2, 2024
ebcd61f
use Callable for exception_handlers
tekumara Sep 4, 2024
c1b05dd
fix mypy lints
tekumara Sep 4, 2024
9cfe2bd
fix ruff lints
tekumara Sep 4, 2024
aa2a879
Merge branch 'master' into exception-handler-typing
tekumara Oct 28, 2024
5673b98
Merge branch 'master' into exception-handler-typing
tekumara Jan 19, 2025
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
6 changes: 3 additions & 3 deletions starlette/_exception_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from starlette.types import ASGIApp, ExceptionHandler, Message, Receive, Scope, Send
from starlette.websockets import WebSocket

ExceptionHandlers = dict[typing.Any, ExceptionHandler]
StatusHandlers = dict[int, ExceptionHandler]
ExceptionHandlers = dict[typing.Any, ExceptionHandler[Exception]]
StatusHandlers = dict[int, ExceptionHandler[Exception]]


def _lookup_exception_handler(exc_handlers: ExceptionHandlers, exc: Exception) -> ExceptionHandler | None:
def _lookup_exception_handler(exc_handlers: ExceptionHandlers, exc: Exception) -> ExceptionHandler[Exception] | None:
for cls in type(exc).__mro__:
if cls in exc_handlers:
return exc_handlers[cls]
Expand Down
17 changes: 13 additions & 4 deletions starlette/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import BaseRoute, Router
from starlette.types import ASGIApp, ExceptionHandler, Lifespan, Receive, Scope, Send
from starlette.types import (
ASGIApp,
ExceptionHandler,
ExceptionType,
Lifespan,
Receive,
Scope,
Send,
)
from starlette.websockets import WebSocket

AppType = typing.TypeVar("AppType", bound="Starlette")
Expand All @@ -32,7 +40,8 @@ def __init__(
debug: bool = False,
routes: typing.Sequence[BaseRoute] | None = None,
middleware: typing.Sequence[Middleware] | None = None,
exception_handlers: typing.Mapping[typing.Any, ExceptionHandler] | None = None,
exception_handlers: typing.Mapping[typing.Any, typing.Callable[[typing.Any, typing.Any], typing.Any]]
| None = None,
on_startup: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
on_shutdown: typing.Sequence[typing.Callable[[], typing.Any]] | None = None,
lifespan: Lifespan[AppType] | None = None,
Expand Down Expand Up @@ -132,8 +141,8 @@ def add_middleware(

def add_exception_handler(
self,
exc_class_or_status_code: int | type[Exception],
handler: ExceptionHandler,
exc_class_or_status_code: int | type[ExceptionType],
handler: ExceptionHandler[ExceptionType],
) -> None: # pragma: no cover
self.exception_handlers[exc_class_or_status_code] = handler

Expand Down
11 changes: 8 additions & 3 deletions starlette/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
StatefulLifespan = typing.Callable[[AppType], typing.AsyncContextManager[typing.Mapping[str, typing.Any]]]
Lifespan = typing.Union[StatelessLifespan[AppType], StatefulLifespan[AppType]]

HTTPExceptionHandler = typing.Callable[["Request", Exception], "Response | typing.Awaitable[Response]"]
WebSocketExceptionHandler = typing.Callable[["WebSocket", Exception], typing.Awaitable[None]]
ExceptionHandler = typing.Union[HTTPExceptionHandler, WebSocketExceptionHandler]
ExceptionType = typing.TypeVar("ExceptionType", bound=Exception)

HTTPExceptionHandler = typing.Callable[["Request", ExceptionType], "Response | typing.Awaitable[Response]"]
WebSocketExceptionHandler = typing.Callable[["WebSocket", ExceptionType], typing.Awaitable[None]]
ExceptionHandler = typing.Union[
HTTPExceptionHandler[ExceptionType],
WebSocketExceptionHandler[ExceptionType],
]
Loading