Skip to content

Commit e3989c4

Browse files
Copilotbrunoborges
andcommitted
Update documentation for EventErrorPolicy and silent error handling
Co-authored-by: brunoborges <129743+brunoborges@users.noreply.github.com>
1 parent 45ac4c3 commit e3989c4

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ session.setEventErrorHandler((event, exception) -> {
4040
});
4141
```
4242

43+
#### EventErrorPolicy for Dispatch Control
44+
Added `EventErrorPolicy` enum to control whether event dispatch continues or stops when a handler throws an exception. The default policy is `CONTINUE` (existing behavior). Set `STOP` to short-circuit dispatch on the first error:
45+
46+
```java
47+
session.setEventErrorPolicy(EventErrorPolicy.STOP);
48+
```
49+
50+
The `EventErrorHandler` is always invoked regardless of the policy. When no error handler is set, exceptions are silently consumed (no logging).
51+
4352
#### Type-Safe Event Handlers
4453
Promoted type-safe `on(Class<T>, Consumer<T>)` event handlers as the primary API. Handlers now receive strongly-typed events instead of raw `AbstractSessionEvent`.
4554

‎src/site/markdown/advanced.md‎

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ This guide covers advanced scenarios for extending and customizing your Copilot
3232
- [Error Handling](#Error_Handling)
3333
- [Event Handler Exceptions](#Event_Handler_Exceptions)
3434
- [Custom Event Error Handler](#Custom_Event_Error_Handler)
35+
- [Event Error Policy](#Event_Error_Policy)
3536

3637
---
3738

@@ -451,9 +452,9 @@ session.send(new MessageOptions().setPrompt("Hello"))
451452

452453
### Event Handler Exceptions
453454

454-
If an event handler throws an exception, the SDK catches it, logs it at
455-
`SEVERE` level, and continues dispatching to remaining handlers. This means
456-
one faulty handler will never block others from receiving events:
455+
If an event handler registered via `session.on()` throws an exception, the SDK
456+
catches it and continues dispatching to remaining handlers by default. This
457+
means one faulty handler will never block others from receiving events:
457458

458459
```java
459460
// This handler throws, but the second handler still runs
@@ -467,17 +468,14 @@ session.on(AssistantMessageEvent.class, msg -> {
467468
});
468469
```
469470

470-
> **Note:** This exception isolation behavior is consistent with the Node.js,
471-
> Go, and Python Copilot SDKs, which all catch handler errors per-handler. The
472-
> .NET SDK is an exception — handler errors propagate there and can prevent
473-
> subsequent handlers from running.
471+
When no `EventErrorHandler` is set, exceptions are **silently consumed** — the
472+
SDK does not log by default. Applications should set an error handler if they
473+
want visibility into handler failures (see below).
474474

475475
### Custom Event Error Handler
476476

477-
By default, handler exceptions are logged at `SEVERE` level using
478-
`java.util.logging`. You can replace this with a custom
479-
`EventErrorHandler` to integrate with your own logging, metrics, or
480-
error-reporting systems:
477+
Set a custom `EventErrorHandler` to integrate with your own logging, metrics,
478+
or error-reporting systems:
481479

482480
```java
483481
session.setEventErrorHandler((event, exception) -> {
@@ -489,10 +487,51 @@ session.setEventErrorHandler((event, exception) -> {
489487

490488
The error handler receives both the event that was being dispatched and the
491489
exception that was thrown. If the error handler itself throws, that exception
492-
is silently caught and logged to prevent cascading failures.
490+
is caught and logged at `SEVERE`, and dispatch is stopped to prevent cascading
491+
failures.
493492

494-
Pass `null` to restore the default logging behavior:
493+
Pass `null` to restore the default silent behavior:
495494

496495
```java
497496
session.setEventErrorHandler(null);
498497
```
498+
499+
### Event Error Policy
500+
501+
By default, the SDK continues dispatching to remaining handlers after an error
502+
(`EventErrorPolicy.CONTINUE`). You can opt in to **short-circuit** behavior so
503+
that the first handler error stops dispatch:
504+
505+
```java
506+
session.setEventErrorPolicy(EventErrorPolicy.STOP);
507+
```
508+
509+
The `EventErrorHandler` (if set) is always invoked regardless of the policy —
510+
the policy only controls whether remaining handlers execute after the error
511+
handler returns.
512+
513+
| Policy | Behavior |
514+
|---|---|
515+
| `CONTINUE` (default) | All remaining handlers execute despite errors |
516+
| `STOP` | Dispatch halts after the first handler error |
517+
518+
You can combine both for full control:
519+
520+
```java
521+
// Log errors and stop dispatch on first failure
522+
session.setEventErrorPolicy(EventErrorPolicy.STOP);
523+
session.setEventErrorHandler((event, ex) ->
524+
logger.error("Handler failed, stopping: {}", ex.getMessage(), ex));
525+
```
526+
527+
Or switch policies dynamically:
528+
529+
```java
530+
// Start lenient
531+
session.setEventErrorPolicy(EventErrorPolicy.CONTINUE);
532+
533+
// Later, switch to strict mode
534+
session.setEventErrorPolicy(EventErrorPolicy.STOP);
535+
```
536+
537+
See [EventErrorPolicy](apidocs/com/github/copilot/sdk/EventErrorPolicy.html) and [EventErrorHandler](apidocs/com/github/copilot/sdk/EventErrorHandler.html) Javadoc for details.

0 commit comments

Comments
 (0)