Skip to content

Commit a113688

Browse files
committed
Enhance documentation: add exception isolation details for event handlers
1 parent 4f80c5e commit a113688

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/main/java/com/github/copilot/sdk/CopilotSession.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ public CompletableFuture<AssistantMessageEvent> sendAndWait(MessageOptions optio
297297
* instead.
298298
*
299299
* <p>
300+
* <b>Exception isolation:</b> If a handler throws an exception, the error is
301+
* logged and remaining handlers still execute.
302+
*
303+
* <p>
300304
* <b>Example:</b>
301305
*
302306
* <pre>{@code
@@ -324,6 +328,10 @@ public Closeable on(Consumer<AbstractSessionEvent> handler) {
324328
* matching the specified type.
325329
*
326330
* <p>
331+
* <b>Exception isolation:</b> If a handler throws an exception, the error is
332+
* logged and remaining handlers still execute.
333+
*
334+
* <p>
327335
* <b>Example Usage</b>
328336
* </p>
329337
*
@@ -367,7 +375,10 @@ public <T extends AbstractSessionEvent> Closeable on(Class<T> eventType, Consume
367375
/**
368376
* Dispatches an event to all registered handlers.
369377
* <p>
370-
* This is called internally when events are received from the server.
378+
* This is called internally when events are received from the server. Each
379+
* handler is invoked in its own try/catch block so that an exception thrown by
380+
* one handler does not prevent subsequent handlers from executing. Exceptions
381+
* are logged at {@link Level#SEVERE}.
371382
*
372383
* @param event
373384
* the event to dispatch

src/site/markdown/advanced.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,26 @@ session.send(new MessageOptions().setPrompt("Hello"))
419419
return null;
420420
});
421421
```
422+
423+
### Event Handler Exceptions
424+
425+
If an event handler throws an exception, the SDK catches it, logs it at
426+
`SEVERE` level, and continues dispatching to remaining handlers. This means
427+
one faulty handler will never block others from receiving events:
428+
429+
```java
430+
// This handler throws, but the second handler still runs
431+
session.on(AssistantMessageEvent.class, msg -> {
432+
throw new RuntimeException("bug in handler 1");
433+
});
434+
435+
session.on(AssistantMessageEvent.class, msg -> {
436+
// This handler executes normally despite the exception above
437+
System.out.println(msg.getData().getContent());
438+
});
439+
```
440+
441+
> **Note:** This exception isolation behavior is consistent with the Node.js,
442+
> Go, and Python Copilot SDKs, which all catch handler errors per-handler. The
443+
> .NET SDK is an exception — handler errors propagate there and can prevent
444+
> subsequent handlers from running.

src/site/markdown/documentation.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ System.out.println(response.getData().getContent());
5757

5858
For more control, subscribe to events and use `send()`:
5959

60+
> **Exception isolation:** If a handler throws an exception, the SDK logs the
61+
> error and continues dispatching to remaining handlers. One misbehaving handler
62+
> will never prevent others from executing.
63+
6064
```java
6165
var done = new CompletableFuture<Void>();
6266

0 commit comments

Comments
 (0)