Skip to content

Commit

Permalink
🐛 suppress error when shutting down
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored May 26, 2024
1 parent eb2db25 commit 126e3ad
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/providers/playwright/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
@Author : yanyongyu
@Date : 2022-09-14 14:22:39
@LastEditors : yanyongyu
@LastEditTime : 2024-05-23 16:55:13
@LastEditTime : 2024-05-26 17:09:44
@Description : Playwright provider plugin
@GitHub : https://github.com/yanyongyu
"""

__author__ = "yanyongyu"

from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from contextlib import contextmanager, asynccontextmanager

from nonebot import get_driver
from nonebot import logger, get_driver
from playwright.async_api import Page, Browser, Playwright, async_playwright

driver = get_driver()
Expand All @@ -22,6 +22,14 @@
"""Global playwright browser instance used to control multiple pages."""


@contextmanager
def _suppress_and_log():
try:
yield
except Exception as e:
logger.opt(exception=e).warning("An error occurred during playwright shutdown.")


@driver.on_startup
async def start_browser():
global _playwright
Expand All @@ -33,9 +41,11 @@ async def start_browser():
@driver.on_shutdown
async def shutdown_browser():
if _browser:
await _browser.close()
with _suppress_and_log():
await _browser.close()
if _playwright:
await _playwright.stop()
with _suppress_and_log():
await _playwright.stop()


def get_browser() -> Browser:
Expand Down

0 comments on commit 126e3ad

Please sign in to comment.