nonebot2/nonebot/exception.py

213 lines
4.7 KiB
Python
Raw Normal View History

2020-08-20 15:07:05 +08:00
"""
2022-01-12 19:24:01 +08:00
## 异常
2020-08-20 15:07:05 +08:00
下列文档中的异常是所有 NoneBot 运行时可能会抛出的
这些异常并非所有需要用户处理 NoneBot 内部运行时被捕获并进行对应操作
"""
2020-06-30 10:13:58 +08:00
from typing import Any, Optional
2021-11-19 18:18:53 +08:00
2021-12-06 00:17:52 +08:00
from pydantic.fields import ModelField
2020-12-03 13:22:12 +08:00
class NoneBotException(Exception):
"""
2022-01-12 18:16:05 +08:00
所有 NoneBot 发生的异常基类
2020-12-03 13:22:12 +08:00
"""
2021-11-19 18:18:53 +08:00
# Rule Exception
class ParserExit(NoneBotException):
"""
2022-01-12 18:19:21 +08:00
`shell command` 处理消息失败时返回的异常
2021-11-19 18:18:53 +08:00
"""
def __init__(self, status: int = 0, message: Optional[str] = None):
self.status = status
self.message = message
def __repr__(self):
return f"<ParserExit status={self.status} message={self.message}>"
def __str__(self):
return self.__repr__()
# Processor Exception
class ProcessException(NoneBotException):
"""
2022-01-12 18:16:05 +08:00
事件处理过程中发生的异常基类
"""
class IgnoredException(ProcessException):
2020-07-25 12:28:30 +08:00
"""
2022-01-12 18:16:05 +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
"""
def __init__(self, reason):
self.reason = reason
2020-08-20 15:07:05 +08:00
def __repr__(self):
return f"<IgnoredException, reason={self.reason}>"
def __str__(self):
return self.__repr__()
2020-07-25 12:28:30 +08:00
class MockApiException(ProcessException):
"""
2022-01-12 18:16:05 +08:00
指示 NoneBot 阻止本次 API 调用或修改本次调用返回值并返回自定义内容可由 api hook 抛出
2022-01-12 18:31:12 +08:00
参数:
2022-01-12 19:10:29 +08:00
result: 返回的内容
"""
def __init__(self, result: Any):
self.result = result
def __repr__(self):
return f"<ApiCancelledException, result={self.result}>"
def __str__(self):
return self.__repr__()
class StopPropagation(ProcessException):
2021-02-02 11:59:14 +08:00
"""
2022-01-12 18:16:05 +08:00
指示 NoneBot 终止事件向下层传播
2021-02-02 11:59:14 +08:00
2022-01-12 18:53:30 +08:00
用法:
2022-01-12 18:19:21 +08:00
`Matcher.block == True` 时抛出
2021-02-02 11:59:14 +08:00
"""
2021-11-19 18:18:53 +08:00
# Matcher Exceptions
class MatcherException(NoneBotException):
"""
2022-01-12 18:16:05 +08:00
所有 Matcher 发生的异常基类
2021-11-19 18:18:53 +08:00
"""
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 SkippedException(MatcherException):
2020-08-20 15:07:05 +08:00
"""
2022-01-12 18:19:21 +08:00
指示 NoneBot 立即结束当前 `Handler` 的处理继续处理下一个 `Handler`
2020-08-20 15:07:05 +08:00
2022-01-12 18:53:30 +08:00
用法:
可以在 `Handler` 中通过 `Matcher.skip()` 抛出
2020-08-20 15:07:05 +08:00
"""
2020-05-02 20:03:36 +08:00
2021-12-29 21:52:46 +08:00
class TypeMisMatch(SkippedException):
"""
2022-01-12 18:19:21 +08:00
当前 `Handler` 的参数类型不匹配
2021-12-29 21:52:46 +08:00
"""
2021-12-06 00:17:52 +08:00
def __init__(self, param: ModelField, value: Any):
2021-12-08 16:46:10 +08:00
self.param: ModelField = param
self.value: Any = value
2021-12-06 00:17:52 +08:00
def __repr__(self):
2021-12-29 21:52:46 +08:00
return f"<TypeMisMatch, param={self.param}, value={self.value}>"
2021-12-06 00:17:52 +08:00
def __str__(self):
self.__repr__()
2020-05-02 20:03:36 +08:00
2021-11-19 18:18:53 +08:00
class PausedException(MatcherException):
2020-08-20 15:07:05 +08:00
"""
2022-01-12 18:19:21 +08:00
指示 NoneBot 结束当前 `Handler` 并等待下一条消息后继续下一个 `Handler`
2021-11-19 18:18:53 +08:00
可用于用户输入新信息
2020-08-20 15:07:05 +08:00
2022-01-12 18:53:30 +08:00
用法:
可以在 `Handler` 中通过 `Matcher.pause()` 抛出
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):
2020-08-20 15:07:05 +08:00
"""
2022-01-12 18:19:21 +08:00
指示 NoneBot 结束当前 `Handler` 并等待下一条消息后重新运行当前 `Handler`
2021-11-19 18:18:53 +08:00
可用于用户重新输入
2020-08-20 15:07:05 +08:00
2022-01-12 18:53:30 +08:00
用法:
可以在 `Handler` 中通过 `Matcher.reject()` 抛出
"""
2021-11-19 18:18:53 +08:00
class FinishedException(MatcherException):
"""
2022-01-12 18:19:21 +08:00
指示 NoneBot 结束当前 `Handler` 且后续 `Handler` 不再被运行
2021-11-19 18:18:53 +08:00
可用于结束用户会话
2022-01-12 18:53:30 +08:00
用法:
可以在 `Handler` 中通过 `Matcher.finish()` 抛出
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):
"""
2022-01-12 18:19:21 +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
"""
def __init__(self, adapter_name: str) -> None:
self.adapter_name = adapter_name
2021-11-19 18:18:53 +08:00
class NoLogException(AdapterException):
2020-12-09 14:39:25 +08:00
"""
2022-01-12 18:19:21 +08:00
指示 NoneBot 对当前 `Event` 进行处理但不显示 Log 信息可在 `get_log_string` 时抛出
2020-12-09 14:39:25 +08:00
"""
2020-12-09 14:39:25 +08:00
pass
2020-12-03 13:22:12 +08:00
class ApiNotAvailable(AdapterException):
2020-08-20 15:07:05 +08:00
"""
2022-01-12 18:16:05 +08:00
API 连接不可用时抛出
2020-08-20 15:07:05 +08:00
"""
2020-08-01 22:03:40 +08:00
pass
2020-08-13 15:23:04 +08:00
2020-12-03 13:22:12 +08:00
class NetworkError(AdapterException):
2020-08-20 15:07:05 +08:00
"""
2022-01-12 18:16:05 +08:00
在网络出现问题时抛出: API 请求地址不正确, API 请求无返回或返回状态非正常等
2020-08-20 15:07:05 +08:00
"""
2020-08-13 15:23:04 +08:00
pass
2020-12-03 13:22:12 +08:00
class ActionFailed(AdapterException):
2020-08-20 15:07:05 +08:00
"""
2022-01-12 18:16:05 +08:00
API 请求成功返回数据 API 操作失败
2020-08-20 15:07:05 +08:00
"""
pass
# Driver Exceptions
class DriverException(NoneBotException):
"""
2022-01-12 18:19:21 +08:00
`Driver` 抛出的异常基类
"""
class WebSocketClosed(DriverException):
"""
2022-01-12 18:16:05 +08:00
WebSocket 连接已关闭
"""
def __init__(self, code: int, reason: Optional[str] = None):
self.code = code
self.reason = reason
def __repr__(self) -> str:
return f"<WebSocketClosed code={self.code} reason={self.reason}>"