fix: catch plugin event handler errors and provide logger to plugins#12929
Open
wilfoa wants to merge 1 commit intoanomalyco:devfrom
Open
fix: catch plugin event handler errors and provide logger to plugins#12929wilfoa wants to merge 1 commit intoanomalyco:devfrom
wilfoa wants to merge 1 commit intoanomalyco:devfrom
Conversation
Plugin event handlers that throw errors or call console.error() can corrupt the TUI display since stderr output appears inline in the terminal. This change: 1. Wraps plugin event handler calls in try-catch so unhandled exceptions are logged to the log file instead of crashing or corrupting the TUI. 2. Adds a `log` property to PluginInput, giving plugins a proper way to log messages to OpenCode's log file instead of using console.error(). 3. Adds `await` to the event handler call so errors are properly caught. 4. Adds tests proving: - Throwing plugins don't crash the bus event loop - Async rejections are caught - A throwing plugin doesn't prevent other plugins from receiving events - Errors are logged via Log system, not printed to stderr - The log property is available to plugins with all expected methods
Contributor
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
Contributor
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found |
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.
Fixes #12931
Summary
awaitso that both sync throws and async rejections are caught instead of crashing the TUI or going uncaught.logproperty onPluginInputso plugins can log to OpenCode's log file instead of writing to stderr (which corrupts the TUI display).Problem
Plugin
eventhandlers registered viaBus.subscribeAllwere called without error handling:throwin a handler would propagate up and crash the bus subscriber.awaited.console.error(), which writes to stderr and corrupts the TUI output.This was observed in practice when a Telegram notification plugin's API call failed — the error message was rendered directly into the TUI chat window.
Changes
packages/opencode/src/plugin/index.tsLog.create({ service: "plugin.user" })log(withinfo,warn,error,debugmethods) to plugins viaPluginInputhook["event"]?.()call in try-catch withawait, logging errors via the loggerpackages/plugin/src/index.tsPluginLoggertype withinfo,warn,error,debugmethodslog: PluginLoggerto thePluginInputinterfacepackages/opencode/test/plugin/event-error-handling.test.tslogproperty is provided with all expected methodsAll 908 tests pass. Typecheck passes across all 12 packages.