mirror of
				https://github.com/nonebot/nonebot2.git
				synced 2025-10-30 22:46:40 +00:00 
			
		
		
		
	🏗️ change exception structure
This commit is contained in:
		| @@ -10,6 +10,7 @@ CQHTTP (OneBot) v11 协议适配 | ||||
|     https://github.com/howmanybots/onebot/blob/master/README.md | ||||
| """ | ||||
|  | ||||
| from .message import Message, MessageSegment | ||||
| from .bot import Bot | ||||
| from .utils import log | ||||
| from .event import Event | ||||
| from .message import Message, MessageSegment | ||||
| from .bot import Bot, _check_at_me, _check_nickname, _check_reply | ||||
|   | ||||
| @@ -8,15 +8,16 @@ import httpx | ||||
|  | ||||
| from nonebot.log import logger | ||||
| from nonebot.config import Config | ||||
| from nonebot.message import handle_event | ||||
| from nonebot.typing import overrides, Driver, WebSocket, NoReturn | ||||
| from nonebot.typing import Any, Dict, Union, Optional | ||||
| from nonebot.adapters import BaseBot | ||||
| from nonebot.exception import NetworkError, ActionFailed, RequestDenied, ApiNotAvailable | ||||
| from nonebot.message import handle_event | ||||
| from nonebot.typing import Any, Dict, Union, Optional | ||||
| from nonebot.typing import overrides, Driver, WebSocket, NoReturn | ||||
| from nonebot.exception import NetworkError, RequestDenied, ApiNotAvailable | ||||
|  | ||||
| from .message import Message, MessageSegment | ||||
| from .utils import log, get_auth_bearer | ||||
| from .event import Event | ||||
| from .exception import ApiError | ||||
| from .utils import log, get_auth_bearer | ||||
| from .message import Message, MessageSegment | ||||
|  | ||||
|  | ||||
| async def _check_reply(bot: "Bot", event: "Event"): | ||||
| @@ -159,11 +160,11 @@ def _handle_api_result( | ||||
|  | ||||
|     :异常: | ||||
|  | ||||
|         - ``ActionFailed``: API 调用失败 | ||||
|         - ``ApiError``: API 调用失败 | ||||
|     """ | ||||
|     if isinstance(result, dict): | ||||
|         if result.get("status") == "failed": | ||||
|             raise ActionFailed(retcode=result.get("retcode")) | ||||
|             raise ApiError(retcode=result.get("retcode")) | ||||
|         return result.get("data") | ||||
|  | ||||
|  | ||||
| @@ -317,7 +318,7 @@ class Bot(BaseBot): | ||||
|         :异常: | ||||
|  | ||||
|           - ``NetworkError``: 网络错误 | ||||
|           - ``ActionFailed``: API 调用失败 | ||||
|           - ``ApiError``: API 调用失败 | ||||
|         """ | ||||
|         if "self_id" in data: | ||||
|             self_id = data.pop("self_id") | ||||
| @@ -368,8 +369,8 @@ class Bot(BaseBot): | ||||
|  | ||||
|     @overrides(BaseBot) | ||||
|     async def send(self, | ||||
|                    event: "Event", | ||||
|                    message: Union[str, "Message", "MessageSegment"], | ||||
|                    event: Event, | ||||
|                    message: Union[str, Message, MessageSegment], | ||||
|                    at_sender: bool = False, | ||||
|                    **kwargs) -> Union[Any, NoReturn]: | ||||
|         """ | ||||
| @@ -392,7 +393,7 @@ class Bot(BaseBot): | ||||
|  | ||||
|           - ``ValueError``: 缺少 ``user_id``, ``group_id`` | ||||
|           - ``NetworkError``: 网络错误 | ||||
|           - ``ActionFailed``: API 调用失败 | ||||
|           - ``ApiError``: API 调用失败 | ||||
|         """ | ||||
|         msg = message if isinstance(message, Message) else Message(message) | ||||
|  | ||||
|   | ||||
							
								
								
									
										29
									
								
								nonebot/adapters/cqhttp/exception.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								nonebot/adapters/cqhttp/exception.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| from nonebot.exception import AdapterException, ActionFailed | ||||
|  | ||||
|  | ||||
| class CQHTTPAdapterException(AdapterException): | ||||
|  | ||||
|     def __init__(self): | ||||
|         super().__init__("cqhttp") | ||||
|  | ||||
|  | ||||
| class ApiError(CQHTTPAdapterException, ActionFailed): | ||||
|     """ | ||||
|     :说明: | ||||
|  | ||||
|       API 请求返回错误信息。 | ||||
|  | ||||
|     :参数: | ||||
|  | ||||
|       * ``retcode: Optional[int]``: 错误码 | ||||
|     """ | ||||
|  | ||||
|     def __init__(self, retcode: Optional[int] = None): | ||||
|         super().__init__() | ||||
|         self.retcode = retcode | ||||
|  | ||||
|     def __repr__(self): | ||||
|         return f"<ActionFailed retcode={self.retcode}>" | ||||
|  | ||||
|     def __str__(self): | ||||
|         return self.__repr__() | ||||
		Reference in New Issue
	
	Block a user