🥅 return event handling exception

This commit is contained in:
Mix 2021-04-08 23:35:08 +08:00 committed by GitHub
parent 916d620012
commit f5a22b3d4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,7 @@ NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供
import asyncio import asyncio
from datetime import datetime 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.log import logger
from nonebot.rule import TrieRule from nonebot.rule import TrieRule
@ -174,7 +174,7 @@ async def _run_matcher(Matcher: Type[Matcher], bot: "Bot", event: "Event",
return 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: if show_log:
logger.debug("Running PreProcessors...") logger.debug("Running PreProcessors...")
await asyncio.gather(*coros) await asyncio.gather(*coros)
except IgnoredException: except IgnoredException as e:
logger.opt(colors=True).info( logger.opt(colors=True).info(
f"Event {event.get_event_name()} is <b>ignored</b>") f"Event {event.get_event_name()} is <b>ignored</b>"
return )
return e
except Exception as e: except Exception as e:
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(
"<r><bg #f8bbd0>Error when running EventPreProcessors. " "<r><bg #f8bbd0>Error when running EventPreProcessors. "
"Event ignored!</bg #f8bbd0></r>") "Event ignored!</bg #f8bbd0></r>"
return )
return e
# Trie Match # Trie Match
_, _ = TrieRule.get_value(bot, event, state) _, _ = 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) results = await asyncio.gather(*pending_tasks, return_exceptions=True)
for result in results: for result in results:
if not isinstance(result, Exception):
continue
if isinstance(result, StopPropagation): if isinstance(result, StopPropagation):
if not break_flag:
break_flag = True break_flag = True
logger.debug("Stop event propagation") logger.debug("Stop event propagation")
elif isinstance(result, Exception): else:
logger.opt(colors=True, exception=result).error( logger.opt(colors=True, exception=result).error(
"<r><bg #f8bbd0>Error when checking Matcher.</bg #f8bbd0></r>" "<r><bg #f8bbd0>Error when checking Matcher.</bg #f8bbd0></r>"
) )
return result
coros = list(map(lambda x: x(bot, event, state), _event_postprocessors)) coros = list(map(lambda x: x(bot, event, state), _event_postprocessors))
if coros: if coros:
@ -256,3 +260,4 @@ async def handle_event(bot: "Bot", event: "Event"):
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(
"<r><bg #f8bbd0>Error when running EventPostProcessors</bg #f8bbd0></r>" "<r><bg #f8bbd0>Error when running EventPostProcessors</bg #f8bbd0></r>"
) )
return e