-
-
Notifications
You must be signed in to change notification settings - Fork 866
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master' into kristjan/reject
- Loading branch information
Showing
32 changed files
with
1,093 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
The starting point for contributions should usually be [a discussion](https://github.com/encode/starlette/discussions) | ||
<!-- Thanks for contributing to Starlette! 💚 | ||
Given this is a project maintained by volunteers, please read this template to not waste your time, or ours! 😁 --> | ||
|
||
Simple documentation typos may be raised as stand-alone pull requests, but otherwise please ensure you've discussed your proposal prior to issuing a pull request. | ||
# Summary | ||
|
||
This will help us direct work appropriately, and ensure that any suggested changes have been okayed by the maintainers. | ||
<!-- Write a small summary about what is happening here. --> | ||
|
||
- [ ] Initially raised as discussion #... | ||
# Checklist | ||
|
||
- [ ] I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!) | ||
- [ ] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. | ||
- [ ] I've updated the documentation accordingly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.26.1" | ||
__version__ = "0.28.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import typing | ||
|
||
from starlette._utils import is_async_callable | ||
from starlette.concurrency import run_in_threadpool | ||
from starlette.exceptions import HTTPException | ||
from starlette.requests import Request | ||
from starlette.responses import Response | ||
from starlette.types import ASGIApp, Message, Receive, Scope, Send | ||
from starlette.websockets import WebSocket | ||
|
||
Handler = typing.Callable[..., typing.Any] | ||
ExceptionHandlers = typing.Dict[typing.Any, Handler] | ||
StatusHandlers = typing.Dict[int, Handler] | ||
|
||
|
||
def _lookup_exception_handler( | ||
exc_handlers: ExceptionHandlers, exc: Exception | ||
) -> typing.Optional[Handler]: | ||
for cls in type(exc).__mro__: | ||
if cls in exc_handlers: | ||
return exc_handlers[cls] | ||
return None | ||
|
||
|
||
def wrap_app_handling_exceptions( | ||
app: ASGIApp, conn: typing.Union[Request, WebSocket] | ||
) -> ASGIApp: | ||
exception_handlers: ExceptionHandlers | ||
status_handlers: StatusHandlers | ||
try: | ||
exception_handlers, status_handlers = conn.scope["starlette.exception_handlers"] | ||
except KeyError: | ||
exception_handlers, status_handlers = {}, {} | ||
|
||
async def wrapped_app(scope: Scope, receive: Receive, send: Send) -> None: | ||
response_started = False | ||
|
||
async def sender(message: Message) -> None: | ||
nonlocal response_started | ||
|
||
if message["type"] == "http.response.start": | ||
response_started = True | ||
await send(message) | ||
|
||
try: | ||
await app(scope, receive, sender) | ||
except Exception as exc: | ||
handler = None | ||
|
||
if isinstance(exc, HTTPException): | ||
handler = status_handlers.get(exc.status_code) | ||
|
||
if handler is None: | ||
handler = _lookup_exception_handler(exception_handlers, exc) | ||
|
||
if handler is None: | ||
raise exc | ||
|
||
if response_started: | ||
msg = "Caught handled exception, but response already started." | ||
raise RuntimeError(msg) from exc | ||
|
||
if scope["type"] == "http": | ||
response: Response | ||
if is_async_callable(handler): | ||
response = await handler(conn, exc) | ||
else: | ||
response = await run_in_threadpool(handler, conn, exc) | ||
await response(scope, receive, sender) | ||
elif scope["type"] == "websocket": | ||
if is_async_callable(handler): | ||
await handler(conn, exc) | ||
else: | ||
await run_in_threadpool(handler, conn, exc) | ||
|
||
return wrapped_app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.