🐛 Fix: Bot Hook 没有捕获跳过异常 (#905)

This commit is contained in:
Ju4tCode
2022-04-04 10:35:14 +08:00
committed by GitHub
parent 494b9c625d
commit 2f3324ce0c
12 changed files with 333 additions and 303 deletions

View File

@ -4,9 +4,10 @@ from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, Any, Set, Dict, Type, Callable, AsyncGenerator
from nonebot.log import logger
from nonebot.utils import escape_tag
from nonebot.config import Env, Config
from nonebot.dependencies import Dependent
from nonebot.exception import SkippedException
from nonebot.utils import escape_tag, run_coro_with_catch
from nonebot.typing import T_BotConnectionHook, T_BotDisconnectionHook
from nonebot.internal.params import BotParam, DependParam, DefaultParam
@ -128,7 +129,12 @@ class Driver(abc.ABC):
self._clients[bot.self_id] = bot
async def _run_hook(bot: "Bot") -> None:
coros = list(map(lambda x: x(bot=bot), self._bot_connection_hook))
coros = list(
map(
lambda x: run_coro_with_catch(x(bot=bot), (SkippedException,)),
self._bot_connection_hook,
)
)
if coros:
try:
await asyncio.gather(*coros)
@ -146,7 +152,12 @@ class Driver(abc.ABC):
del self._clients[bot.self_id]
async def _run_hook(bot: "Bot") -> None:
coros = list(map(lambda x: x(bot=bot), self._bot_disconnection_hook))
coros = list(
map(
lambda x: run_coro_with_catch(x(bot=bot), (SkippedException,)),
self._bot_disconnection_hook,
)
)
if coros:
try:
await asyncio.gather(*coros)

View File

@ -248,8 +248,8 @@ class Cookies(MutableMapping):
self,
name: str,
default: Optional[str] = None,
domain: str = None,
path: str = None,
domain: Optional[str] = None,
path: Optional[str] = None,
) -> Optional[str]:
value: Optional[str] = None
for cookie in self.jar:

View File

@ -3,6 +3,7 @@ from contextlib import AsyncExitStack
from typing import Any, Set, Tuple, Union, NoReturn, Optional, Coroutine
from nonebot.dependencies import Dependent
from nonebot.utils import run_coro_with_catch
from nonebot.exception import SkippedException
from nonebot.typing import T_DependencyCache, T_PermissionChecker
@ -10,13 +11,6 @@ from .adapter import Bot, Event
from .params import BotParam, EventParam, DependParam, DefaultParam
async def _run_coro_with_catch(coro: Coroutine[Any, Any, Any]):
try:
return await coro
except SkippedException:
return False
class Permission:
"""{ref}`nonebot.matcher.Matcher` 权限类。
@ -72,13 +66,15 @@ class Permission:
return True
results = await asyncio.gather(
*(
_run_coro_with_catch(
run_coro_with_catch(
checker(
bot=bot,
event=event,
stack=stack,
dependency_cache=dependency_cache,
)
),
(SkippedException,),
False,
)
for checker in self.checkers
),