2022-01-18 23:46:10 +08:00
|
|
|
|
"""本模块包含了所有 NoneBot 运行时可能会抛出的异常。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
|
|
|
|
这些异常并非所有需要用户处理,在 NoneBot 内部运行时被捕获,并进行对应操作。
|
2022-01-16 11:30:09 +08:00
|
|
|
|
|
2022-01-18 16:12:12 +08:00
|
|
|
|
```bash
|
|
|
|
|
NoneBotException
|
|
|
|
|
├── ParserExit
|
|
|
|
|
├── ProcessException
|
|
|
|
|
| ├── IgnoredException
|
2022-04-04 10:35:14 +08:00
|
|
|
|
| ├── SkippedException
|
|
|
|
|
| | └── TypeMisMatch
|
2022-01-18 16:12:12 +08:00
|
|
|
|
| ├── MockApiException
|
|
|
|
|
| └── StopPropagation
|
|
|
|
|
├── MatcherException
|
|
|
|
|
| ├── PausedException
|
|
|
|
|
| ├── RejectedException
|
|
|
|
|
| └── FinishedException
|
|
|
|
|
├── AdapterException
|
|
|
|
|
| ├── NoLogException
|
|
|
|
|
| ├── ApiNotAvailable
|
|
|
|
|
| ├── NetworkError
|
|
|
|
|
| └── ActionFailed
|
|
|
|
|
└── DriverException
|
|
|
|
|
└── WebSocketClosed
|
|
|
|
|
```
|
|
|
|
|
|
2022-01-16 11:30:09 +08:00
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
|
mdx:
|
|
|
|
|
format: md
|
2022-01-16 11:30:09 +08:00
|
|
|
|
sidebar_position: 10
|
|
|
|
|
description: nonebot.exception 模块
|
2020-08-20 15:07:05 +08:00
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2021-11-28 02:29:23 +08:00
|
|
|
|
from typing import Any, Optional
|
2021-11-19 18:18:53 +08:00
|
|
|
|
|
2024-01-26 11:12:57 +08:00
|
|
|
|
from nonebot.compat import ModelField
|
2021-12-06 00:17:52 +08:00
|
|
|
|
|
2020-08-21 14:24:32 +08:00
|
|
|
|
|
2020-12-03 13:22:12 +08:00
|
|
|
|
class NoneBotException(Exception):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""所有 NoneBot 发生的异常基类。"""
|
2020-12-03 13:22:12 +08:00
|
|
|
|
|
2022-12-31 20:41:53 +08:00
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
return self.__repr__()
|
|
|
|
|
|
2020-12-03 13:22:12 +08:00
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
# Rule Exception
|
|
|
|
|
class ParserExit(NoneBotException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""{ref}`nonebot.rule.shell_command` 处理消息失败时返回的异常。"""
|
2021-11-19 18:18:53 +08:00
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
def __init__(self, status: int = 0, message: Optional[str] = None) -> None:
|
2021-11-19 18:18:53 +08:00
|
|
|
|
self.status = status
|
|
|
|
|
self.message = message
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
f"ParserExit(status={self.status}"
|
|
|
|
|
+ (f", message={self.message!r}" if self.message else "")
|
|
|
|
|
+ ")"
|
|
|
|
|
)
|
2021-11-19 18:18:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Processor Exception
|
2021-11-28 02:29:23 +08:00
|
|
|
|
class ProcessException(NoneBotException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""事件处理过程中发生的异常基类。"""
|
2021-11-28 02:29:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IgnoredException(ProcessException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""指示 NoneBot 应该忽略该事件。可由 PreProcessor 抛出。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
reason: 忽略事件的原因
|
2020-07-25 12:28:30 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
def __init__(self, reason: Any) -> None:
|
2022-01-18 16:12:12 +08:00
|
|
|
|
self.reason: Any = reason
|
2020-07-25 12:28:30 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"IgnoredException(reason={self.reason!r})"
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2020-07-25 12:28:30 +08:00
|
|
|
|
|
2022-04-04 10:35:14 +08:00
|
|
|
|
class SkippedException(ProcessException):
|
|
|
|
|
"""指示 NoneBot 立即结束当前 `Dependent` 的运行。
|
|
|
|
|
|
|
|
|
|
例如,可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.skip` 抛出。
|
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```python
|
|
|
|
|
def always_skip():
|
|
|
|
|
Matcher.skip()
|
|
|
|
|
|
|
|
|
|
@matcher.handle()
|
|
|
|
|
async def handler(dependency = Depends(always_skip)):
|
|
|
|
|
# never run
|
|
|
|
|
```
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TypeMisMatch(SkippedException):
|
|
|
|
|
"""当前 `Handler` 的参数类型不匹配。"""
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
def __init__(self, param: ModelField, value: Any) -> None:
|
2022-04-04 10:35:14 +08:00
|
|
|
|
self.param: ModelField = param
|
|
|
|
|
self.value: Any = value
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
|
|
|
|
f"TypeMisMatch(param={self.param.name}, "
|
|
|
|
|
f"type={self.param._type_display()}, value={self.value!r}>"
|
|
|
|
|
)
|
2022-04-04 10:35:14 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-28 02:29:23 +08:00
|
|
|
|
class MockApiException(ProcessException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""指示 NoneBot 阻止本次 API 调用或修改本次调用返回值,并返回自定义内容。
|
|
|
|
|
可由 api hook 抛出。
|
2021-11-28 02:29:23 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
result: 返回的内容
|
2021-11-28 02:29:23 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, result: Any):
|
|
|
|
|
self.result = result
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"MockApiException(result={self.result!r})"
|
2021-11-28 02:29:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StopPropagation(ProcessException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""指示 NoneBot 终止事件向下层传播。
|
|
|
|
|
|
|
|
|
|
在 {ref}`nonebot.matcher.Matcher.block` 为 `True`
|
|
|
|
|
或使用 {ref}`nonebot.matcher.Matcher.stop_propagation` 方法时抛出。
|
2021-02-02 11:59:14 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
2022-01-18 16:12:12 +08:00
|
|
|
|
```python
|
|
|
|
|
matcher = on_notice(block=True)
|
|
|
|
|
# 或者
|
|
|
|
|
@matcher.handle()
|
|
|
|
|
async def handler(matcher: Matcher):
|
|
|
|
|
matcher.stop_propagation()
|
|
|
|
|
```
|
2021-02-02 11:59:14 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
# Matcher Exceptions
|
|
|
|
|
class MatcherException(NoneBotException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""所有 Matcher 发生的异常基类。"""
|
2021-02-02 12:39:23 +08:00
|
|
|
|
|
2021-02-02 11:59:14 +08:00
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
class PausedException(MatcherException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""指示 NoneBot 结束当前 `Handler` 并等待下一条消息后继续下一个 `Handler`。
|
|
|
|
|
可用于用户输入新信息。
|
2022-01-18 16:12:12 +08:00
|
|
|
|
|
|
|
|
|
可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.pause` 抛出。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
2022-01-18 16:12:12 +08:00
|
|
|
|
```python
|
|
|
|
|
@matcher.handle()
|
|
|
|
|
async def handler():
|
|
|
|
|
await matcher.pause("some message")
|
|
|
|
|
```
|
2020-08-20 15:07:05 +08:00
|
|
|
|
"""
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
class RejectedException(MatcherException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""指示 NoneBot 结束当前 `Handler` 并等待下一条消息后重新运行当前 `Handler`。
|
|
|
|
|
可用于用户重新输入。
|
2022-01-18 16:12:12 +08:00
|
|
|
|
|
|
|
|
|
可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.reject` 抛出。
|
2020-08-20 15:07:05 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
2022-01-18 16:12:12 +08:00
|
|
|
|
```python
|
|
|
|
|
@matcher.handle()
|
|
|
|
|
async def handler():
|
|
|
|
|
await matcher.reject("some message")
|
|
|
|
|
```
|
2020-08-21 14:24:32 +08:00
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
class FinishedException(MatcherException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""指示 NoneBot 结束当前 `Handler` 且后续 `Handler` 不再被运行。可用于结束用户会话。
|
|
|
|
|
|
|
|
|
|
可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.finish` 抛出。
|
2020-08-21 14:24:32 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
2022-01-18 16:12:12 +08:00
|
|
|
|
```python
|
|
|
|
|
@matcher.handle()
|
|
|
|
|
async def handler():
|
|
|
|
|
await matcher.finish("some message")
|
|
|
|
|
```
|
2020-08-20 15:07:05 +08:00
|
|
|
|
"""
|
2020-08-01 22:03:40 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
# Adapter Exceptions
|
2020-12-03 13:22:12 +08:00
|
|
|
|
class AdapterException(NoneBotException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""代表 `Adapter` 抛出的异常,所有的 `Adapter` 都要在内部继承自这个 `Exception`。
|
2020-12-03 13:22:12 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
adapter_name: 标识 adapter
|
2020-12-03 13:22:12 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __init__(self, adapter_name: str, *args: object) -> None:
|
|
|
|
|
super().__init__(*args)
|
2022-01-18 16:12:12 +08:00
|
|
|
|
self.adapter_name: str = adapter_name
|
2020-12-03 13:22:12 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-19 18:18:53 +08:00
|
|
|
|
class NoLogException(AdapterException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""指示 NoneBot 对当前 `Event` 进行处理但不显示 Log 信息。
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
可在 {ref}`nonebot.adapters.Event.get_log_string` 时抛出
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""
|
2020-12-09 14:39:25 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-03 13:22:12 +08:00
|
|
|
|
class ApiNotAvailable(AdapterException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""在 API 连接不可用时抛出。"""
|
2020-08-13 15:23:04 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-03 13:22:12 +08:00
|
|
|
|
class NetworkError(AdapterException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""在网络出现问题时抛出,
|
|
|
|
|
如: API 请求地址不正确, API 请求无返回或返回状态非正常等。
|
|
|
|
|
"""
|
2020-08-13 15:23:04 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-03 13:22:12 +08:00
|
|
|
|
class ActionFailed(AdapterException):
|
2022-01-18 16:12:12 +08:00
|
|
|
|
"""API 请求成功返回数据,但 API 操作失败。"""
|
2021-12-26 14:20:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Driver Exceptions
|
|
|
|
|
class DriverException(NoneBotException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""`Driver` 抛出的异常基类。"""
|
2021-12-26 14:20:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketClosed(DriverException):
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""WebSocket 连接已关闭。"""
|
2021-12-26 14:20:09 +08:00
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
def __init__(self, code: int, reason: Optional[str] = None) -> None:
|
2021-12-26 14:20:09 +08:00
|
|
|
|
self.code = code
|
|
|
|
|
self.reason = reason
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2022-09-09 11:52:57 +08:00
|
|
|
|
return (
|
|
|
|
|
f"WebSocketClosed(code={self.code}"
|
|
|
|
|
+ (f", reason={self.reason!r}" if self.reason else "")
|
|
|
|
|
+ ")"
|
|
|
|
|
)
|