mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
🐛 add skip handle for process
This commit is contained in:
parent
c25ee1cb56
commit
ca4d7397f8
@ -156,7 +156,7 @@ async def solve_dependencies(
|
|||||||
f"type {type(value)} not match depends {_dependent.func} "
|
f"type {type(value)} not match depends {_dependent.func} "
|
||||||
f"annotation {field._type_display()}, ignored"
|
f"annotation {field._type_display()}, ignored"
|
||||||
)
|
)
|
||||||
raise SkippedException
|
raise SkippedException(field, value)
|
||||||
else:
|
else:
|
||||||
values[field.name] = value
|
values[field.name] = value
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
|
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from pydantic.fields import ModelField
|
||||||
|
|
||||||
|
|
||||||
class NoneBotException(Exception):
|
class NoneBotException(Exception):
|
||||||
"""
|
"""
|
||||||
@ -124,6 +126,16 @@ class SkippedException(MatcherException):
|
|||||||
可以在 ``Handler`` 中通过 ``Matcher.skip()`` 抛出。
|
可以在 ``Handler`` 中通过 ``Matcher.skip()`` 抛出。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, param: ModelField, value: Any):
|
||||||
|
self.param = param
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<SkippedException, param={self.param}, value={self.value}>"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
self.__repr__()
|
||||||
|
|
||||||
|
|
||||||
class PausedException(MatcherException):
|
class PausedException(MatcherException):
|
||||||
"""
|
"""
|
||||||
|
@ -669,8 +669,11 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
_stack=stack,
|
_stack=stack,
|
||||||
_dependency_cache=dependency_cache,
|
_dependency_cache=dependency_cache,
|
||||||
)
|
)
|
||||||
except SkippedException:
|
except SkippedException as e:
|
||||||
pass
|
logger.debug(
|
||||||
|
f"Handler {handler} param {e.param.name} value {e.value} "
|
||||||
|
f"mismatch type {e.param._type_display()}, skipped"
|
||||||
|
)
|
||||||
|
|
||||||
except RejectedException:
|
except RejectedException:
|
||||||
self.handlers.insert(0, handler) # type: ignore
|
self.handlers.insert(0, handler) # type: ignore
|
||||||
|
@ -8,7 +8,16 @@ NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from contextlib import AsyncExitStack
|
from contextlib import AsyncExitStack
|
||||||
from typing import TYPE_CHECKING, Any, Set, Dict, Type, Optional
|
from typing import (
|
||||||
|
TYPE_CHECKING,
|
||||||
|
Any,
|
||||||
|
Set,
|
||||||
|
Dict,
|
||||||
|
List,
|
||||||
|
Type,
|
||||||
|
Optional,
|
||||||
|
Coroutine,
|
||||||
|
)
|
||||||
|
|
||||||
from nonebot import params
|
from nonebot import params
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
@ -16,7 +25,12 @@ from nonebot.rule import TrieRule
|
|||||||
from nonebot.handler import Handler
|
from nonebot.handler import Handler
|
||||||
from nonebot.utils import escape_tag
|
from nonebot.utils import escape_tag
|
||||||
from nonebot.matcher import Matcher, matchers
|
from nonebot.matcher import Matcher, matchers
|
||||||
from nonebot.exception import NoLogException, StopPropagation, IgnoredException
|
from nonebot.exception import (
|
||||||
|
NoLogException,
|
||||||
|
StopPropagation,
|
||||||
|
IgnoredException,
|
||||||
|
SkippedException,
|
||||||
|
)
|
||||||
from nonebot.typing import (
|
from nonebot.typing import (
|
||||||
T_State,
|
T_State,
|
||||||
T_DependencyCache,
|
T_DependencyCache,
|
||||||
@ -100,6 +114,13 @@ def run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:
|
|||||||
# FIXME: run handler with try/except skipped exception
|
# FIXME: run handler with try/except skipped exception
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_coro_with_catch(coro: Coroutine[Any, Any, Any]) -> Any:
|
||||||
|
try:
|
||||||
|
return await coro
|
||||||
|
except SkippedException:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def _check_matcher(
|
async def _check_matcher(
|
||||||
priority: int,
|
priority: int,
|
||||||
Matcher: Type[Matcher],
|
Matcher: Type[Matcher],
|
||||||
@ -150,13 +171,15 @@ async def _run_matcher(
|
|||||||
|
|
||||||
coros = list(
|
coros = list(
|
||||||
map(
|
map(
|
||||||
lambda x: x(
|
lambda x: _run_coro_with_catch(
|
||||||
matcher=matcher,
|
x(
|
||||||
bot=bot,
|
matcher=matcher,
|
||||||
event=event,
|
bot=bot,
|
||||||
state=state,
|
event=event,
|
||||||
_stack=stack,
|
state=state,
|
||||||
_dependency_cache=dependency_cache,
|
_stack=stack,
|
||||||
|
_dependency_cache=dependency_cache,
|
||||||
|
)
|
||||||
),
|
),
|
||||||
_run_preprocessors,
|
_run_preprocessors,
|
||||||
)
|
)
|
||||||
@ -189,14 +212,16 @@ async def _run_matcher(
|
|||||||
|
|
||||||
coros = list(
|
coros = list(
|
||||||
map(
|
map(
|
||||||
lambda x: x(
|
lambda x: _run_coro_with_catch(
|
||||||
matcher=matcher,
|
x(
|
||||||
exception=exception,
|
matcher=matcher,
|
||||||
bot=bot,
|
exception=exception,
|
||||||
event=event,
|
bot=bot,
|
||||||
state=state,
|
event=event,
|
||||||
_stack=stack,
|
state=state,
|
||||||
_dependency_cache=dependency_cache,
|
_stack=stack,
|
||||||
|
_dependency_cache=dependency_cache,
|
||||||
|
)
|
||||||
),
|
),
|
||||||
_run_postprocessors,
|
_run_postprocessors,
|
||||||
)
|
)
|
||||||
@ -247,12 +272,14 @@ async def handle_event(bot: "Bot", event: "Event") -> None:
|
|||||||
async with AsyncExitStack() as stack:
|
async with AsyncExitStack() as stack:
|
||||||
coros = list(
|
coros = list(
|
||||||
map(
|
map(
|
||||||
lambda x: x(
|
lambda x: _run_coro_with_catch(
|
||||||
bot=bot,
|
x(
|
||||||
event=event,
|
bot=bot,
|
||||||
state=state,
|
event=event,
|
||||||
_stack=stack,
|
state=state,
|
||||||
_dependency_cache=dependency_cache,
|
_stack=stack,
|
||||||
|
_dependency_cache=dependency_cache,
|
||||||
|
)
|
||||||
),
|
),
|
||||||
_event_preprocessors,
|
_event_preprocessors,
|
||||||
)
|
)
|
||||||
@ -307,12 +334,14 @@ async def handle_event(bot: "Bot", event: "Event") -> None:
|
|||||||
|
|
||||||
coros = list(
|
coros = list(
|
||||||
map(
|
map(
|
||||||
lambda x: x(
|
lambda x: _run_coro_with_catch(
|
||||||
bot=bot,
|
x(
|
||||||
event=event,
|
bot=bot,
|
||||||
state=state,
|
event=event,
|
||||||
_stack=stack,
|
state=state,
|
||||||
_dependency_cache=dependency_cache,
|
_stack=stack,
|
||||||
|
_dependency_cache=dependency_cache,
|
||||||
|
)
|
||||||
),
|
),
|
||||||
_event_postprocessors,
|
_event_postprocessors,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user