Liking cljdoc? Tell your friends :D

Error Handling Hook

The onErrorOccurred hook is called when errors occur during session execution. Use it to:

  • Implement custom error logging
  • Track error patterns
  • Provide user-friendly error messages
  • Trigger alerts for critical errors

Hook Signature

Node.js / TypeScript
import type { ErrorOccurredHookInput, HookInvocation, ErrorOccurredHookOutput } from "copilot-sdk-supercharged";
type ErrorOccurredHandler = (
  input: ErrorOccurredHookInput,
  invocation: HookInvocation
) => Promise<ErrorOccurredHookOutput | null | undefined>;
type ErrorOccurredHandler = (
  input: ErrorOccurredHookInput,
  invocation: HookInvocation
) => Promise<ErrorOccurredHookOutput | null | undefined>;
Python
from copilot.session import ErrorOccurredHookInput, ErrorOccurredHookOutput
from copilot.session import ErrorOccurredHandler as SdkErrorOccurredHandler
from typing import Callable, Awaitable

ErrorOccurredHandler = Callable[
    [ErrorOccurredHookInput, dict[str, str]],
    Awaitable[ErrorOccurredHookOutput | None]
]

def check(handler: ErrorOccurredHandler) -> SdkErrorOccurredHandler:
    return handler
ErrorOccurredHandler = Callable[
    [ErrorOccurredHookInput, dict[str, str]],
    Awaitable[ErrorOccurredHookOutput | None]
]
Go
package main

import copilot "github.com/microsoft/copilot-sdk/go/v2"

// Aliases let the signature below use the package's own type names.
type (
    ErrorOccurredHookInput  = copilot.ErrorOccurredHookInput
    HookInvocation          = copilot.HookInvocation
    ErrorOccurredHookOutput = copilot.ErrorOccurredHookOutput
)

type ErrorOccurredHandler func(
    input ErrorOccurredHookInput,
    invocation HookInvocation,
) (*ErrorOccurredHookOutput, error)

var _ = copilot.ErrorOccurredHandler(ErrorOccurredHandler(nil))

func main() {}
type ErrorOccurredHandler func(
    input ErrorOccurredHookInput,
    invocation HookInvocation,
) (*ErrorOccurredHookOutput, error)
.NET
using Microsoft.Copilot;

public delegate Task<ErrorOccurredHookOutput?> ErrorOccurredHandler(
    ErrorOccurredHookInput input,
    HookInvocation invocation);

static class ErrorOccurredHandlerCheck
{
    static SessionHooks Check(ErrorOccurredHandler handler) =>
        new() { OnErrorOccurred = handler.Invoke };
}
public delegate Task<ErrorOccurredHookOutput?> ErrorOccurredHandler(
    ErrorOccurredHookInput input,
    HookInvocation invocation);
Java
// Note: Java SDK does not have an onErrorOccurred hook.
// Use EventErrorPolicy and EventErrorHandler instead:
//
// session.setEventErrorPolicy(EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS);
// session.setEventErrorHandler((event, ex) -> {
//     System.err.println("Error in " + event.getType() + ": " + ex.getMessage());
// });
//
// See the "Basic Error Logging" example below for a complete snippet.
Rust
type ErrorOccurredHandler = Box<dyn Fn(
    ErrorOccurredHookInput,
    HookInvocation,
) -> Pin<Box<dyn Future<Output = Option<ErrorOccurredHookOutput>>>> + Send + Sync>;
Ruby
# on_error_occurred receives input hash and invocation hash,
# returns an ErrorOccurredHookOutput hash or nil.
on_error_occurred = ->(input, invocation) { nil }
PHP
// ErrorOccurredHandler receives $input and $invocation arrays,
// returns an ErrorOccurredHookOutput array or null.
$onErrorOccurred = function (array $input, array $invocation): ?array {
    return null;
};
Swift
typealias ErrorOccurredHandler = (
    ErrorOccurredHookInput,
    HookInvocation
) async throws -> ErrorOccurredHookOutput?
Kotlin
typealias ErrorOccurredHandler = suspend (
    input: ErrorOccurredHookInput,
    invocation: HookInvocation
) -> ErrorOccurredHookOutput?
C++
using ErrorOccurredHandler = std::function<
    std::optional<ErrorOccurredHookOutput>(
        const ErrorOccurredHookInput& input,
        const HookInvocation& invocation
    )>;

40 languages supported. See the full SDK list with cookbooks for Objective-C, F#, Groovy, Julia, COBOL, OCaml, Zig, Nim, D, Erlang, Crystal, Tcl, Solidity, V, and 18 more.

Input

FieldTypeDescription
timestampSDK timestamp typeWhen the error occurred
workingDirectorystringCurrent working directory
errorstringError message
errorContextstringWhere the error occurred: "model_call", "tool_execution", "system", or "user_input"
recoverablebooleanWhether the error can potentially be recovered from

Output

Return null or undefined to use default error handling. Otherwise, return an object with:

FieldTypeDescription
suppressOutputbooleanIf true, don't show error output to user
errorHandlingstringHow to handle: "retry", "skip", or "abort"
retryCountnumberNumber of times to retry (if errorHandling is "retry")
userNotificationstringCustom message to show the user

Examples

Basic Error Logging

Node.js / TypeScript
const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input, invocation) => {
                console.error(`[${invocation.sessionId}] Error: ${input.error}`);
                console.error(`  Context: ${input.errorContext}`);
                console.error(`  Recoverable: ${input.recoverable}`);
                return null;
            },
        },
    },
});
Python
from copilot import CopilotClient
from copilot.session import PermissionHandler

client = CopilotClient(mode="coding-agent")


async def on_error_occurred(input_data, invocation):
    print(f"[{invocation['session_id']}] Error: {input_data['error']}")
    print(f"  Context: {input_data['errorContext']}")
    print(f"  Recoverable: {input_data['recoverable']}")
    return None


session = await client.create_session(
    permissions={"on_request": PermissionHandler.approve_all},
    hooks={"handlers": {"on_error_occurred": on_error_occurred}},
)
Go
package main

import (
	"context"
	"fmt"
	copilot "github.com/microsoft/copilot-sdk/go/v2"
)

func main() {
	client := copilot.NewClient(&copilot.ClientOptions{Mode: copilot.ModeCodingAgent})
	session, _ := client.CreateSession(context.Background(), &copilot.CreateSessionOptions{
		Hooks: copilot.SessionHookOptions{
			Handlers: &copilot.SessionHooks{
				OnErrorOccurred: func(input copilot.ErrorOccurredHookInput, inv copilot.HookInvocation) (*copilot.ErrorOccurredHookOutput, error) {
					fmt.Printf("[%s] Error: %s\n", inv.SessionID, input.Error)
					fmt.Printf("  Context: %s\n", input.ErrorContext)
					fmt.Printf("  Recoverable: %v\n", input.Recoverable)
					return nil, nil
				},
			},
		},
	})
	_ = session
}
session, _ := client.CreateSession(context.Background(), &copilot.CreateSessionOptions{
	Hooks: copilot.SessionHookOptions{
		Handlers: &copilot.SessionHooks{
			OnErrorOccurred: func(input copilot.ErrorOccurredHookInput, inv copilot.HookInvocation) (*copilot.ErrorOccurredHookOutput, error) {
				fmt.Printf("[%s] Error: %s\n", inv.SessionID, input.Error)
				fmt.Printf("  Context: %s\n", input.ErrorContext)
				fmt.Printf("  Recoverable: %v\n", input.Recoverable)
				return nil, nil
			},
		},
	},
})

.NET
var session = await client.CreateSessionAsync(new CreateSessionOptions
{
    Hooks = new SessionHookOptions
    {
        Handlers = new SessionHooks
        {
            OnErrorOccurred = (input, invocation) =>
            {
                Console.Error.WriteLine($"[{invocation.SessionId}] Error: {input.Error}");
                Console.Error.WriteLine($"  Context: {input.ErrorContext}");
                Console.Error.WriteLine($"  Recoverable: {input.Recoverable}");
                return Task.FromResult<ErrorOccurredHookOutput?>(null);
            },
        }
    }
});
Java
import com.microsoft.copilot.*;
import com.microsoft.copilot.rpc.*;

public class ErrorHandlingExample {
    public static void main(String[] args) throws Exception {
        try (var client = new CopilotClient(new CopilotClientOptions().setMode(CopilotClientMode.CODING_AGENT))) {
            var session = client
                    .createSession(new CreateSessionOptions()
                            .setPermissions(new SessionPermissionOptions()
                                    .setOnRequest(PermissionHandler.APPROVE_ALL)))
                    .get();

            session
                    .setEventErrorPolicy(EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS);
            session
                    .setEventErrorHandler((event, ex) -> {
                        System.err
                                .println("[" + session
                                        .getSessionId() + "] Error: "
                                        + ex
                                                .getMessage());
                        System.err
                                .println("  Event: " + event
                                        .getType());
                    });
        }
    }
}
import com.microsoft.copilot.*;
import com.microsoft.copilot.rpc.*;

// Note: Java SDK does not have an onErrorOccurred hook.
// Use EventErrorPolicy and EventErrorHandler instead:

var session = client
        .createSession(new CreateSessionOptions()
                .setPermissions(new SessionPermissionOptions()
                        .setOnRequest(PermissionHandler.APPROVE_ALL)))
        .get();

session
        .setEventErrorPolicy(EventErrorPolicy.SUPPRESS_AND_LOG_ERRORS);
session
        .setEventErrorHandler((event, ex) -> {
            System.err
                    .println("[" + session
                            .getSessionId() + "] Error: "
                            + ex
                                    .getMessage());
            System.err
                    .println("  Event: " + event
                            .getType());
        });
Rust
let session = client.create_session(&SessionConfig {
    hooks: Some(SessionHooks {
        on_error_occurred: Some(Box::new(|input, invocation| {
            Box::pin(async move {
                eprintln!("[{}] Error: {}", invocation.session_id, input.error);
                eprintln!("  Context: {}", input.error_context);
                eprintln!("  Recoverable: {}", input.recoverable);
                None
            })
        })),
        ..Default::default()
    }),
    ..Default::default()
}).await?;
Ruby
session = client.create_session(
  on_permission_request: ->(_req, _inv) { { kind: "approved" } },
  hooks: {
    on_error_occurred: ->(input, invocation) {
      warn "[#{invocation[:session_id]}] Error: #{input[:error]}"
      warn "  Context: #{input[:errorContext]}"
      warn "  Recoverable: #{input[:recoverable]}"
      nil
    }
  }
)
PHP
$session = $client->createSession([
    'hooks' => [
        'onErrorOccurred' => function (array $input, array $invocation): ?array {
            fwrite(STDERR, "[{$invocation['sessionId']}] Error: {$input['error']}\n");
            fwrite(STDERR, "  Context: {$input['errorContext']}\n");
            fwrite(STDERR, "  Recoverable: {$input['recoverable']}\n");
            return null;
        },
    ],
]);
Swift
let session = try await client.createSession(config: SessionConfig(
    hooks: SessionHooks(
        onErrorOccurred: { input, invocation in
            print("[\\(invocation.sessionId)] Error: \\(input.error)", to: &stderr)
            print("  Context: \\(input.errorContext)", to: &stderr)
            print("  Recoverable: \\(input.recoverable)", to: &stderr)
            return nil
        }
    )
))
Kotlin
val session = client.createSession(SessionConfig(
    hooks = SessionHooks(
        onErrorOccurred = { input, invocation ->
            System.err.println("[${invocation.sessionId}] Error: ${input.error}")
            System.err.println("  Context: ${input.errorContext}")
            System.err.println("  Recoverable: ${input.recoverable}")
            null
        }
    )
))
C++
auto session = client.createSession({
    .hooks = {
        .onErrorOccurred = [](const ErrorOccurredHookInput& input,
                              const HookInvocation& invocation)
            -> std::optional<ErrorOccurredHookOutput> {
            std::cerr << "[" << invocation.sessionId << "] Error: " << input.error << "\n";
            std::cerr << "  Context: " << input.errorContext << "\n";
            std::cerr << "  Recoverable: " << input.recoverable << "\n";
            return std::nullopt;
        },
    },
});

Send Errors to Monitoring Service

import { captureException } from "@sentry/node"; // or your monitoring service

const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input, invocation) => {
                captureException(new Error(input.error), {
                    tags: {
                        sessionId: invocation.sessionId,
                        errorContext: input.errorContext,
                    },
                    extra: {
                        error: input.error,
                        recoverable: input.recoverable,
                        cwd: input.workingDirectory,
                    },
                });

                return null;
            },
        },
    },
});

User-Friendly Error Messages

const ERROR_MESSAGES: Record<string, string> = {
    model_call: "There was an issue communicating with the AI model. Please try again.",
    tool_execution: "A tool failed to execute. Please check your inputs and try again.",
    system: "A system error occurred. Please try again later.",
    user_input: "There was an issue with your input. Please check and try again.",
};

const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input) => {
                const friendlyMessage = ERROR_MESSAGES[input.errorContext];

                if (friendlyMessage) {
                    return {
                        userNotification: friendlyMessage,
                    };
                }

                return null;
            },
        },
    },
});

Suppress Non-Critical Errors

const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input) => {
                // Suppress tool execution errors that are recoverable
                if (input.errorContext === "tool_execution" && input.recoverable) {
                    console.log(`Suppressed recoverable error: ${input.error}`);
                    return { suppressOutput: true };
                }
                return null;
            },
        },
    },
});

Add Recovery Context

const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input) => {
                if (input.errorContext === "tool_execution") {
                    return {
                        userNotification: `
The tool failed. Here are some recovery suggestions:
- Check if required dependencies are installed
- Verify file paths are correct
- Try a simpler approach
          `.trim(),
                    };
                }

                if (input.errorContext === "model_call" && input.error.includes("rate")) {
                    return {
                        errorHandling: "retry",
                        retryCount: 3,
                        userNotification: "Rate limit hit. Retrying...",
                    };
                }

                return null;
            },
        },
    },
});

Track Error Patterns

interface ErrorStats {
    count: number;
    lastOccurred: Date;
    contexts: string[];
}

const errorStats = new Map<string, ErrorStats>();

const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input, invocation) => {
                const key = `${input.errorContext}:${input.error.substring(0, 50)}`;

                const existing = errorStats.get(key) || {
                    count: 0,
                    lastOccurred: input.timestamp,
                    contexts: [],
                };

                existing.count++;
                existing.lastOccurred = input.timestamp;
                existing.contexts.push(invocation.sessionId);

                errorStats.set(key, existing);

                // Alert if error is recurring
                if (existing.count >= 5) {
                    console.warn(`Recurring error detected: ${key} (${existing.count} times)`);
                }

                return null;
            },
        },
    },
});

Alert on Critical Errors

const CRITICAL_CONTEXTS = ["system", "model_call"];

const session = await client.createSession({
    hooks: {
        handlers: {
            onErrorOccurred: async (input, invocation) => {
                if (CRITICAL_CONTEXTS.includes(input.errorContext) && !input.recoverable) {
                    await sendAlert({
                        level: "critical",
                        message: `Critical error in session ${invocation.sessionId}`,
                        error: input.error,
                        context: input.errorContext,
                        timestamp: input.timestamp.toISOString(),
                    });
                }

                return null;
            },
        },
    },
});

Combine with Other Hooks for Context

const sessionContext = new Map<string, { lastTool?: string; lastPrompt?: string }>();

const session = await client.createSession({
    hooks: {
        handlers: {
            onPreToolUse: async (input, invocation) => {
                const ctx = sessionContext.get(invocation.sessionId) || {};
                ctx.lastTool = input.toolName;
                sessionContext.set(invocation.sessionId, ctx);
                return { permissionDecision: "allow" };
            },

            onUserPromptSubmitted: async (input, invocation) => {
                const ctx = sessionContext.get(invocation.sessionId) || {};
                ctx.lastPrompt = input.prompt.substring(0, 100);
                sessionContext.set(invocation.sessionId, ctx);
                return null;
            },

            onErrorOccurred: async (input, invocation) => {
                const ctx = sessionContext.get(invocation.sessionId);

                console.error(`Error in session ${invocation.sessionId}:`);
                console.error(`  Error: ${input.error}`);
                console.error(`  Context: ${input.errorContext}`);
                if (ctx?.lastTool) {
                    console.error(`  Last tool: ${ctx.lastTool}`);
                }
                if (ctx?.lastPrompt) {
                    console.error(`  Last prompt: ${ctx.lastPrompt}...`);
                }

                return null;
            },
        },
    },
});

Best Practices

  1. Always log errors - Even if you suppress them from users, keep logs for debugging.

  2. Categorize errors - Use errorContext to handle different errors appropriately.

  3. Don't swallow critical errors - Only suppress errors you're certain are non-critical.

  4. Keep hooks fast - Error handling shouldn't slow down recovery.

  5. Provide helpful context - When a tool call fails, return additionalContext from an onPostToolUseFailure hook to help the model recover.

  6. Monitor error patterns - Track recurring errors to identify systemic issues.

See Also

Can you improve this documentation? These fine people already did:
Jeremiah Isaacson, Patrick Nikoletich, copilot-cli-release-app[bot], Brett Cannon, Bruno Borges, copilot-sdk-mirror-writer[bot], Sunbrye Ly, Ed Burns, Steve Sanderson & Andy Feller
Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close