Skip to content

fix: propagate dispatch error to current task #917

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

Merged
merged 2 commits into from
Oct 1, 2021
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: 14 additions & 9 deletions playwright/_impl/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ async def inner_send(
if params is None:
params = {}
callback = self._connection._send_message_to_server(self._guid, method, params)

done, pending = await asyncio.wait(
{self._connection._transport.on_error_future, callback.future},
if self._connection._error:
error = self._connection._error
self._connection._error = None
raise error
done, _ = await asyncio.wait(
{
self._connection._transport.on_error_future,
callback.future,
},
return_when=asyncio.FIRST_COMPLETED,
)
if not callback.future.done():
Expand Down Expand Up @@ -152,10 +158,10 @@ def __init__(
self._callbacks: Dict[int, ProtocolCallback] = {}
self._object_factory = object_factory
self._is_sync = False
self._api_name = ""
self._child_ws_connections: List["Connection"] = []
self._loop = loop
self._playwright_future: asyncio.Future["Playwright"] = loop.create_future()
self._error: Optional[BaseException] = None

async def run_as_sync(self) -> None:
self._is_sync = True
Expand Down Expand Up @@ -260,11 +266,10 @@ def _dispatch(self, msg: ParsedMessagePayload) -> None:
g.switch(self._replace_guids_with_channels(params))
else:
object._channel.emit(method, self._replace_guids_with_channels(params))
except Exception:
print(
"Error dispatching the event",
"".join(traceback.format_exception(*sys.exc_info())),
)
except BaseException as exc:
print("Error occured in event listener", file=sys.stderr)
traceback.print_exc()
self._error = exc

def _create_remote_object(
self, parent: ChannelOwner, type: str, guid: str, initializer: Dict
Expand Down
18 changes: 18 additions & 0 deletions tests/common/test_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Dict

import pytest

from playwright.sync_api import sync_playwright


def test_events(browser_name: str, launch_arguments: Dict) -> None:
with pytest.raises(Exception, match="fail"):

def fail() -> None:
raise Exception("fail")

with sync_playwright() as p:
with p[browser_name].launch(**launch_arguments) as browser:
with browser.new_page() as page:
page.on("response", lambda _: fail())
page.goto("https://example.com")