Skip to content

Commit

Permalink
Handle navigation errors and retry (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykeremy committed Apr 17, 2024
1 parent 3c06d44 commit 0b7378f
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions skyvern/webeye/browser_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,42 @@ async def check_and_fix_state(self, url: str | None = None) -> None:
assert self.browser_context is not None

if self.page is None:
LOG.info("Creating a new page")
self.page = await self.browser_context.new_page()

await self._close_all_other_pages()
LOG.info("A new page is created")
if url:
LOG.info(f"Navigating page to {url} and waiting for 3 seconds")
await asyncio.sleep(3)
success = False
retries = 0

while not success and retries < 3:
try:
await self.page.goto(url)
except Error as playright_error:
LOG.exception(f"Error while navigating to url: {str(playright_error)}", exc_info=True)
raise FailedToNavigateToUrl(url=url, error_message=str(playright_error))
LOG.info(f"Successfully went to {url}")
LOG.info("Creating a new page")
self.page = await self.browser_context.new_page()
await self._close_all_other_pages()
LOG.info("A new page is created")
if url:
LOG.info(f"Navigating page to {url} and waiting for 3 seconds")
try:
await self.page.goto(url)
await asyncio.sleep(3)
except Error as playright_error:
LOG.exception(f"Error while navigating to url: {str(playright_error)}", exc_info=True)
raise FailedToNavigateToUrl(url=url, error_message=str(playright_error))
success = True
LOG.info(f"Successfully went to {url}")
else:
success = True
except Exception as e:
LOG.exception(
f"Error while creating or navigating to a new page. Waiting for 5 seconds. Error: {str(e)}",
exc_info=True,
)
retries += 1
# Wait for 5 seconds before retrying
await asyncio.sleep(5)
if retries >= 3:
LOG.exception(f"Failed to create a new page after 3 retries: {str(e)}", exc_info=True)
raise e
LOG.info(f"Retrying to create a new page. Retry count: {retries}")

if self.browser_artifacts.video_path is None:
self.browser_artifacts.video_path = await self.page.video.path()
self.browser_artifacts.video_path = await self.page.video.path() if self.page and self.page.video else None

async def get_or_create_page(self, url: str | None = None) -> Page:
await self.check_and_fix_state(url)
Expand Down

0 comments on commit 0b7378f

Please sign in to comment.