feat(core): add plugin error hook for custom error routing#5192
Merged
Soulter merged 2 commits intoAstrBotDevs:masterfrom Feb 18, 2026
Merged
feat(core): add plugin error hook for custom error routing#5192Soulter merged 2 commits intoAstrBotDevs:masterfrom
Soulter merged 2 commits intoAstrBotDevs:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Hey - 我发现了 1 个问题
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `astrbot/core/pipeline/process_stage/method/star_request.py:55-64` </location>
<code_context>
logger.error(f"Star {handler.handler_full_name} handle error: {e}")
- if event.is_at_or_wake_command:
+ suppress_default_error_reply = await call_event_hook(
+ event,
+ EventType.OnPluginErrorEvent,
+ md.name,
+ handler.handler_name,
+ e,
+ traceback_text,
+ )
+
+ if not suppress_default_error_reply and event.is_at_or_wake_command:
ret = f":(\n\n在调用插件 {md.name} 的处理函数 {handler.handler_name} 时出现异常:{e}"
event.set_result(MessageEventResult().message(ret))
</code_context>
<issue_to_address>
**suggestion (bug_risk):** 将错误回复抑制逻辑与文档中描述的 `event.stop_event()` 行为对齐。
`register_on_plugin_error` 的文档字符串承诺 `event.stop_event()` 会抑制默认错误回复,但当前实现却依赖于 `call_event_hook` 的返回值(`suppress_default_error_reply`)。这会让行为绑定到一个辅助函数的返回约定,而不是事件本身的状态。
可以考虑在调用钩子之后检查事件状态:
```python
await call_event_hook(...)
if not event.is_stopped and event.is_at_or_wake_command:
...
```
或者,如果你打算继续使用返回值,请确保 `call_event_hook` 明确返回 `event.is_stopped`,并在文档中说明这一约定,以便与文档字符串保持一致。
</issue_to_address>帮我变得更有用!请对每条评论点 👍 或 👎,我会根据这些反馈改进你的代码评审体验。
Original comment in English
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `astrbot/core/pipeline/process_stage/method/star_request.py:55-64` </location>
<code_context>
logger.error(f"Star {handler.handler_full_name} handle error: {e}")
- if event.is_at_or_wake_command:
+ suppress_default_error_reply = await call_event_hook(
+ event,
+ EventType.OnPluginErrorEvent,
+ md.name,
+ handler.handler_name,
+ e,
+ traceback_text,
+ )
+
+ if not suppress_default_error_reply and event.is_at_or_wake_command:
ret = f":(\n\n在调用插件 {md.name} 的处理函数 {handler.handler_name} 时出现异常:{e}"
event.set_result(MessageEventResult().message(ret))
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Align error-reply suppression logic with the documented `event.stop_event()` behavior.
The docstring for `register_on_plugin_error` promises that `event.stop_event()` suppresses the default error reply, but this implementation instead relies on `call_event_hook`’s return value (`suppress_default_error_reply`). That ties behavior to a helper’s return convention rather than the event state.
Consider checking the event state after the hook call:
```python
await call_event_hook(...)
if not event.is_stopped and event.is_at_or_wake_command:
...
```
Alternatively, if you keep using the return value, ensure `call_event_hook` explicitly returns `event.is_stopped` and document that contract so it matches the docstring.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Soulter
approved these changes
Feb 18, 2026
united-pooh
pushed a commit
to united-pooh/AstrBot
that referenced
this pull request
Feb 19, 2026
…vs#5192) * feat(core): add plugin error hook for custom error routing * fix(core): align plugin error suppression with event stop state
Raven95676
added a commit
to Raven95676/AstrBot_Rdev
that referenced
this pull request
Feb 20, 2026
- Integrate active_event_registry register/unregister into new PipelineExecutor to restore AstrBotDevs#5225 (terminate active events on reset/new/del) - Add missing component validators (Json, Share, Music, Forward, Location, Contact, Shake, Dice, RPS, Unknown) to SendService to restore AstrBotDevs#5208 (JSON-only message empty reply error) - Invoke OnPluginErrorEvent hook in CommandDispatcher error path to restore AstrBotDevs#5192 (plugin error hook for custom error routing) - Pass text=comp.text when creating TTS Record to restore AstrBotDevs#5204 (voice_messages_forbidden fallback caption on Telegram)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5182
Motivation / 改动动机
之前插件 handler 抛异常后,只能走框架默认报错回显,插件侧无法接管处理。这个在实际使用里有几个明显痛点:群里容易出现不友好的技术报错、插件作者无法把异常定向发到维护会话、不同插件各自处理也容易重复造轮子。
这次增加
OnPluginErrorEvent的目标很直接:把“插件报错后的处理权”交还给插件作者。带来的好处:
显)。
plugin_name、handler_name、traceback_text,定位问题更快。Modifications / 改动点
主要改动文件:
astrbot/core/star/star_handler.py新增事件类型
OnPluginErrorEvent。astrbot/core/star/register/star_handler.py新增
register_on_plugin_error注册器。astrbot/core/star/register/__init__.py导出
register_on_plugin_error。astrbot/api/event/filter/__init__.py导出
on_plugin_error装饰器。astrbot/core/pipeline/process_stage/method/star_request.py插件 handler 报错后触发
OnPluginErrorEvent,并传入:plugin_name、handler_name、error、traceback_text。若钩子侧调用
stop_event(),则不再发送默认报错回显。astrbot/dashboard/routes/plugin.pyWebUI 事件类型文案新增“插件报错时”。
This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
验证目标:确认插件报错时会触发
OnPluginErrorEvent,且可通过stop_event()屏蔽默认报错回显。
验证步骤:
/hook_test_raise(该命令会主动抛出RuntimeError)。关键日志(节选):
结果说明:
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
添加一个插件错误钩子事件,使插件能够拦截并自定义处理由插件处理器抛出的异常。
新增功能:
OnPluginErrorEvent类型,用于表示插件处理器错误事件。register_on_plugin_error装饰器,并通过核心的 register 和 API filter 模块导出,以便插件能订阅插件错误事件。增强内容:
OnPluginErrorEvent,允许钩子选择性地屏蔽框架的默认错误回复。Original summary in English
Summary by Sourcery
Add a plugin error hook event to allow plugins to intercept and customize handling of exceptions thrown by plugin handlers.
New Features:
Enhancements: