[FEATURE]: add tool.execute.error hook to handle tool call errors #7201

Closed
opened 2026-02-16 18:06:26 -05:00 by yindo · 1 comment
Owner

Originally created by @kynnyhsap on GitHub (Jan 22, 2026).

Originally assigned to: @thdxr on GitHub.

Problem

The plugin system has hooks for tool.execute.before and tool.execute.after, allowing plugins to intercept and modify tool inputs/outputs. However, there's no way to hook into tool errors.

When a tool throws an error, plugins cannot:

  • Log or track the error
  • Modify the error message to be more helpful
  • Recover from known errors by returning a fallback result

Use Case

Sometimes you know exactly what an error means and want to handle it gracefully. For example:

  • A file read fails because the file doesn't exist - you might want to return a friendly message instead of a stack trace
  • An API call hits a rate limit - you might want to modify the error to suggest waiting
  • A known transient error occurs - you might want to return a cached/fallback result

Proposed Solution

Add a tool.execute.error hook that fires when tool execution throws:

"tool.execute.error"?: (
  input: { tool: string; sessionID: string; callID: string; args: any },
  output: { 
    error: Error
    result?: { title: string; output: string; metadata: any }
  },
) => Promise<void>

Plugins could either:

  1. Modify output.error to change the error that gets thrown
  2. Set output.result to return a successful result instead of throwing

Example Usage

export default {
  hooks: {
    "tool.execute.error": async (input, output) => {
      console.error(`Tool ${input.tool} failed:`, output.error.message)

      // Option 1: Modify error message
      if (output.error.message.includes("rate limit")) {
        output.error = new Error("Service temporarily unavailable, please try again")
      }

      // Option 2: Recover from error by providing a result
      if (input.tool === "read" && output.error.message.includes("not found")) {
        output.result = {
          title: "File not found",
          output: `The file ${input.args.filePath} does not exist.`,
          metadata: {}
        }
      }
    }
  }
}
Originally created by @kynnyhsap on GitHub (Jan 22, 2026). Originally assigned to: @thdxr on GitHub. ## Problem The plugin system has hooks for `tool.execute.before` and `tool.execute.after`, allowing plugins to intercept and modify tool inputs/outputs. However, there's no way to hook into tool errors. When a tool throws an error, plugins cannot: - Log or track the error - Modify the error message to be more helpful - Recover from known errors by returning a fallback result ## Use Case Sometimes you know exactly what an error means and want to handle it gracefully. For example: - A file read fails because the file doesn't exist - you might want to return a friendly message instead of a stack trace - An API call hits a rate limit - you might want to modify the error to suggest waiting - A known transient error occurs - you might want to return a cached/fallback result ## Proposed Solution Add a `tool.execute.error` hook that fires when tool execution throws: ```typescript "tool.execute.error"?: ( input: { tool: string; sessionID: string; callID: string; args: any }, output: { error: Error result?: { title: string; output: string; metadata: any } }, ) => Promise<void> ``` Plugins could either: 1. Modify `output.error` to change the error that gets thrown 2. Set `output.result` to return a successful result instead of throwing ## Example Usage ```typescript export default { hooks: { "tool.execute.error": async (input, output) => { console.error(`Tool ${input.tool} failed:`, output.error.message) // Option 1: Modify error message if (output.error.message.includes("rate limit")) { output.error = new Error("Service temporarily unavailable, please try again") } // Option 2: Recover from error by providing a result if (input.tool === "read" && output.error.message.includes("not found")) { output.result = { title: "File not found", output: `The file ${input.args.filePath} does not exist.`, metadata: {} } } } } } ```
yindo closed this issue 2026-02-16 18:06:26 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 22, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #8009: Tool errors should be returned to Agent instead of crashing the Action
  • #5148: [FEATURE]: Comprehensive Plugin Pipeline - Middleware-Style Data Flow Control
  • #5894: [BUG] Plugin hooks (tool.execute.before) don't intercept subagent tool calls - security policy bypass

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 22, 2026): This issue might be a duplicate of existing issues. Please check: - #8009: Tool errors should be returned to Agent instead of crashing the Action - #5148: [FEATURE]: Comprehensive Plugin Pipeline - Middleware-Style Data Flow Control - #5894: [BUG] Plugin hooks (tool.execute.before) don't intercept subagent tool calls - security policy bypass Feel free to ignore if none of these address your specific case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7201