fix: handle unavailable aiocqhttp api sends#8922
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces robust error handling for ApiNotAvailable exceptions in the aiocqhttp message dispatcher, falling back to bot.send when the primary sending methods fail, and logging a warning instead of raising an unhandled exception. It also adds unit tests to verify this fallback behavior. The review feedback suggests extending this error handling to forward messages that use bot.call_action and adding an additional test case to cover the scenario where both the primary and fallback send attempts fail.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| raise ValueError( | ||
| f"无法发送消息:缺少有效的数字 session_id({session_id}) 或 event({event})", | ||
| ) | ||
| except ApiNotAvailable: |
There was a problem hiding this comment.
While handling ApiNotAvailable in _dispatch_send is a great improvement, the send_message method also calls bot.call_action directly for forward messages (when isinstance(seg, Node | Nodes)) without wrapping them in a try...except ApiNotAvailable block. If the API is unavailable when sending a forward message, it will still raise an unhandled ApiNotAvailable exception.
Consider wrapping those bot.call_action calls in send_message with a similar try...except ApiNotAvailable block to ensure consistent error handling across all message types.
| user_id=987654, | ||
| message=[{"type": "text", "data": {"text": "hello"}}], | ||
| ) | ||
| bot.send.assert_not_awaited() |
There was a problem hiding this comment.
To ensure full test coverage of the fallback failure path (where both the primary send and the fallback bot.send fail with ApiNotAvailable), we should add a test case verifying that the exception is caught and does not propagate.
bot.send.assert_not_awaited()
@pytest.mark.asyncio
async def test_aiocqhttp_send_message_both_apis_unavailable():
bot = AsyncMock()
bot.send_private_msg.side_effect = ApiNotAvailable
bot.send.side_effect = ApiNotAvailable
event = Event.from_payload(
{
"post_type": "message",
"message_type": "private",
"sub_type": "friend",
"self_id": 12345,
"user_id": 987654,
"message_id": 1,
"message": [],
}
)
chain = MessageChain([Comp.Plain("hello")])
await AiocqhttpMessageEvent.send_message(
bot=bot,
message_chain=chain,
event=event,
is_group=False,
session_id="987654",
)
bot.send_private_msg.assert_awaited_once()
bot.send.assert_awaited_once()
Modifications / 改动点
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Handle aiocqhttp API unavailability more gracefully by falling back to event-based sending and skipping messages instead of raising errors when necessary.
Bug Fixes:
Tests: