🐛 fix matcher.skip missing

This commit is contained in:
yanyongyu 2021-12-29 21:52:46 +08:00
parent c7f428795b
commit 4bd2156929
3 changed files with 26 additions and 11 deletions

View File

@ -15,7 +15,7 @@ from pydantic.fields import Required, FieldInfo, Undefined, ModelField
from nonebot.log import logger
from .utils import get_typed_signature
from nonebot.exception import SkippedException
from nonebot.exception import TypeMisMatch
from nonebot.utils import run_sync, is_coroutine_callable
T = TypeVar("T", bound="Dependent")
@ -182,7 +182,7 @@ class Dependent(Generic[R]):
f"type {type(value)} not match depends {self.call} "
f"annotation {field._type_display()}, ignored"
)
raise SkippedException(field, value)
raise TypeMisMatch(field, value)
else:
values[field.name] = value

View File

@ -126,12 +126,20 @@ class SkippedException(MatcherException):
可以在 ``Handler`` 中通过 ``Matcher.skip()`` 抛出
"""
class TypeMisMatch(SkippedException):
"""
:说明:
当前 ``Handler`` 的参数类型不匹配
"""
def __init__(self, param: ModelField, value: Any):
self.param: ModelField = param
self.value: Any = value
def __repr__(self):
return f"<SkippedException, param={self.param}, value={self.value}>"
return f"<TypeMisMatch, param={self.param}, value={self.value}>"
def __str__(self):
self.__repr__()

View File

@ -42,13 +42,6 @@ from nonebot.consts import (
LAST_RECEIVE_KEY,
REJECT_CACHE_TARGET,
)
from nonebot.exception import (
PausedException,
StopPropagation,
SkippedException,
FinishedException,
RejectedException,
)
from nonebot.typing import (
Any,
T_State,
@ -58,6 +51,14 @@ from nonebot.typing import (
T_DependencyCache,
T_PermissionUpdater,
)
from nonebot.exception import (
TypeMisMatch,
PausedException,
StopPropagation,
SkippedException,
FinishedException,
RejectedException,
)
if TYPE_CHECKING:
from nonebot.plugin import Plugin
@ -646,6 +647,10 @@ class Matcher(metaclass=MatcherMeta):
await cls.send(prompt, **kwargs)
raise RejectedException
@classmethod
def skip(cls) -> NoReturn:
raise SkippedException
def get_receive(self, id: str, default: T = None) -> Union[Event, T]:
return self.state.get(RECEIVE_KEY.format(id=id), default)
@ -729,11 +734,13 @@ class Matcher(metaclass=MatcherMeta):
stack=stack,
dependency_cache=dependency_cache,
)
except SkippedException as e:
except TypeMisMatch as e:
logger.debug(
f"Handler {handler} param {e.param.name} value {e.value} "
f"mismatch type {e.param._type_display()}, skipped"
)
except SkippedException as e:
logger.debug(f"Handler {handler} skipped")
except StopPropagation:
self.block = True
finally: