diff --git a/nonebot/drivers/aiohttp.py b/nonebot/drivers/aiohttp.py index 9d726da8..5c701a94 100644 --- a/nonebot/drivers/aiohttp.py +++ b/nonebot/drivers/aiohttp.py @@ -234,11 +234,20 @@ class Driver(ForwardDriver): try: async with aiohttp.ClientSession() as session: while not self.should_exit.is_set(): - if not bot: + + try: if callable(setup): setup_ = await setup() else: setup_ = setup + except Exception as e: + logger.opt(colors=True, exception=e).error( + f"Error while parsing setup {setup!r}." + ) + await asyncio.sleep(3) + continue + + if not bot: request = await _build_request(setup_) if not request: return @@ -247,7 +256,6 @@ class Driver(ForwardDriver): bot = BotClass(setup.self_id, request) self._bot_connect(bot) elif callable(setup): - setup_ = await setup() request = await _build_request(setup_) if not request: await asyncio.sleep(setup_.poll_interval) @@ -308,10 +316,18 @@ class Driver(ForwardDriver): try: async with aiohttp.ClientSession() as session: while True: - if callable(setup): - setup_ = await setup() - else: - setup_ = setup + + try: + if callable(setup): + setup_ = await setup() + else: + setup_ = setup + except Exception as e: + logger.opt(colors=True, exception=e).error( + f"Error while parsing setup {setup!r}." + ) + await asyncio.sleep(3) + continue url = URL(setup_.url) if not url.is_absolute() or not url.host: diff --git a/nonebot/drivers/fastapi.py b/nonebot/drivers/fastapi.py index 7682b089..62c0cef1 100644 --- a/nonebot/drivers/fastapi.py +++ b/nonebot/drivers/fastapi.py @@ -349,11 +349,20 @@ class Driver(ReverseDriver, ForwardDriver): try: async with httpx.AsyncClient(http2=True) as session: while not self.shutdown.is_set(): - if not bot: + + try: if callable(setup): setup_ = await setup() else: setup_ = setup + except Exception as e: + logger.opt(colors=True, exception=e).error( + f"Error while parsing setup {setup!r}." + ) + await asyncio.sleep(3) + continue + + if not bot: request = await _build_request(setup_) if not request: return @@ -361,7 +370,6 @@ class Driver(ReverseDriver, ForwardDriver): bot = BotClass(setup.self_id, request) self._bot_connect(bot) elif callable(setup): - setup_ = await setup() request = await _build_request(setup_) if not request: await asyncio.sleep(setup_.poll_interval) @@ -406,10 +414,18 @@ class Driver(ReverseDriver, ForwardDriver): try: while True: - if callable(setup): - setup_ = await setup() - else: - setup_ = setup + + try: + if callable(setup): + setup_ = await setup() + else: + setup_ = setup + except Exception as e: + logger.opt(colors=True, exception=e).error( + f"Error while parsing setup {setup!r}." + ) + await asyncio.sleep(3) + continue url = httpx.URL(setup_.url) if not url.netloc: