From f5a22b3d4bc33b89a4383f117ea3e8283c01f08a Mon Sep 17 00:00:00 2001 From: Mix <32300164+mnixry@users.noreply.github.com> Date: Thu, 8 Apr 2021 23:35:08 +0800 Subject: [PATCH] :goal_net: return event handling exception --- nonebot/message.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/nonebot/message.py b/nonebot/message.py index 469b1df2..5292ff5d 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -7,7 +7,7 @@ NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供 import asyncio from datetime import datetime -from typing import Set, Type, TYPE_CHECKING +from typing import Set, Type, TYPE_CHECKING, Optional from nonebot.log import logger from nonebot.rule import TrieRule @@ -174,7 +174,7 @@ async def _run_matcher(Matcher: Type[Matcher], bot: "Bot", event: "Event", return -async def handle_event(bot: "Bot", event: "Event"): +async def handle_event(bot: "Bot", event: "Event") -> Optional[Exception]: """ :说明: @@ -208,15 +208,17 @@ async def handle_event(bot: "Bot", event: "Event"): if show_log: logger.debug("Running PreProcessors...") await asyncio.gather(*coros) - except IgnoredException: + except IgnoredException as e: logger.opt(colors=True).info( - f"Event {event.get_event_name()} is ignored") - return + f"Event {event.get_event_name()} is ignored" + ) + return e except Exception as e: logger.opt(colors=True, exception=e).error( "Error when running EventPreProcessors. " - "Event ignored!") - return + "Event ignored!" + ) + return e # Trie Match _, _ = TrieRule.get_value(bot, event, state) @@ -237,14 +239,16 @@ async def handle_event(bot: "Bot", event: "Event"): results = await asyncio.gather(*pending_tasks, return_exceptions=True) for result in results: + if not isinstance(result, Exception): + continue if isinstance(result, StopPropagation): - if not break_flag: - break_flag = True - logger.debug("Stop event propagation") - elif isinstance(result, Exception): + break_flag = True + logger.debug("Stop event propagation") + else: logger.opt(colors=True, exception=result).error( "Error when checking Matcher." ) + return result coros = list(map(lambda x: x(bot, event, state), _event_postprocessors)) if coros: @@ -256,3 +260,4 @@ async def handle_event(bot: "Bot", event: "Event"): logger.opt(colors=True, exception=e).error( "Error when running EventPostProcessors" ) + return e