forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventErrorHandler.java
More file actions
80 lines (76 loc) · 3.06 KB
/
EventErrorHandler.java
File metadata and controls
80 lines (76 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import com.github.copilot.sdk.events.AbstractSessionEvent;
/**
* A handler for errors thrown by event handlers during event dispatch.
* <p>
* When an event handler registered via
* {@link CopilotSession#on(java.util.function.Consumer)} or
* {@link CopilotSession#on(Class, java.util.function.Consumer)} throws an
* exception, the {@code EventErrorHandler} is invoked with the event that was
* being dispatched and the exception that was thrown.
*
* <p>
* The handler's return value controls whether event dispatch continues to
* remaining handlers:
* <ul>
* <li>Return {@code true} to continue dispatching to remaining handlers
* (default behavior for independent listeners)</li>
* <li>Return {@code false} to stop dispatching (short-circuit behavior for
* validation or critical errors)</li>
* </ul>
*
* <p>
* When no error handler is set, exceptions are silently caught and dispatch
* continues to remaining handlers. This makes the SDK non-intrusive by default.
* Applications should set an error handler to log, track, or respond to handler
* failures.
*
* <p>
* Example configurations:
*
* <pre>{@code
* // Continue on error (log and keep dispatching)
* session.setEventErrorHandler((event, exception) -> {
* logger.error("Handler failed: {}", exception.getMessage(), exception);
* return true; // keep dispatching
* });
*
* // Short-circuit on error (stop at first failure)
* session.setEventErrorHandler((event, exception) -> {
* logger.error("Handler failed, stopping: {}", exception.getMessage(), exception);
* return false; // stop dispatching
* });
*
* // Selective: short-circuit only for critical events
* session.setEventErrorHandler((event, exception) -> {
* logger.error("Handler failed: {}", exception.getMessage(), exception);
* return !(event instanceof SessionErrorEvent); // stop only for error events
* });
* }</pre>
*
* <p>
* If the error handler itself throws an exception, that exception is caught,
* logged at {@link java.util.logging.Level#SEVERE}, and dispatch is stopped to
* prevent cascading failures. When the error handler throws, its return value
* (if it would have been computed) is ignored.
*
* @see CopilotSession#setEventErrorHandler(EventErrorHandler)
* @since 1.0.8
*/
@FunctionalInterface
public interface EventErrorHandler {
/**
* Called when an event handler throws an exception during event dispatch.
*
* @param event
* the event that was being dispatched when the error occurred
* @param exception
* the exception thrown by the event handler
* @return {@code true} to continue dispatching to remaining handlers,
* {@code false} to stop dispatching (the exception is not rethrown)
*/
boolean handleError(AbstractSessionEvent event, Exception exception);
}