|
| 1 | +# Error Handling Patterns |
| 2 | + |
| 3 | +Handle errors gracefully in your Copilot SDK applications. |
| 4 | + |
| 5 | +> **Runnable example:** [recipe/error-handling.cs](recipe/error-handling.cs) |
| 6 | +> |
| 7 | +> ```bash |
| 8 | +> dotnet run recipe/error-handling.cs |
| 9 | +> ``` |
| 10 | +
|
| 11 | +## Example scenario |
| 12 | +
|
| 13 | +You need to handle various error conditions like connection failures, timeouts, and invalid responses. |
| 14 | +
|
| 15 | +## Basic try-catch |
| 16 | +
|
| 17 | +```csharp |
| 18 | +using GitHub.Copilot.SDK; |
| 19 | +
|
| 20 | +var client = new CopilotClient(); |
| 21 | +
|
| 22 | +try |
| 23 | +{ |
| 24 | + await client.StartAsync(); |
| 25 | + var session = await client.CreateSessionAsync(new SessionConfig |
| 26 | + { |
| 27 | + Model = "gpt-5" |
| 28 | + }); |
| 29 | +
|
| 30 | + var done = new TaskCompletionSource<string>(); |
| 31 | + session.On(evt => |
| 32 | + { |
| 33 | + if (evt is AssistantMessageEvent msg) |
| 34 | + { |
| 35 | + done.SetResult(msg.Data.Content); |
| 36 | + } |
| 37 | + }); |
| 38 | +
|
| 39 | + await session.SendAsync(new MessageOptions { Prompt = "Hello!" }); |
| 40 | + var response = await done.Task; |
| 41 | + Console.WriteLine(response); |
| 42 | +
|
| 43 | + await session.DisposeAsync(); |
| 44 | +} |
| 45 | +catch (Exception ex) |
| 46 | +{ |
| 47 | + Console.WriteLine($"Error: {ex.Message}"); |
| 48 | +} |
| 49 | +finally |
| 50 | +{ |
| 51 | + await client.StopAsync(); |
| 52 | +} |
| 53 | +``` |
| 54 | +
|
| 55 | +## Handling specific error types |
| 56 | +
|
| 57 | +```csharp |
| 58 | +try |
| 59 | +{ |
| 60 | + await client.StartAsync(); |
| 61 | +} |
| 62 | +catch (FileNotFoundException) |
| 63 | +{ |
| 64 | + Console.WriteLine("Copilot CLI not found. Please install it first."); |
| 65 | +} |
| 66 | +catch (HttpRequestException ex) when (ex.Message.Contains("connection")) |
| 67 | +{ |
| 68 | + Console.WriteLine("Could not connect to Copilot CLI server."); |
| 69 | +} |
| 70 | +catch (Exception ex) |
| 71 | +{ |
| 72 | + Console.WriteLine($"Unexpected error: {ex.Message}"); |
| 73 | +} |
| 74 | +``` |
| 75 | +
|
| 76 | +## Timeout handling |
| 77 | +
|
| 78 | +```csharp |
| 79 | +var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" }); |
| 80 | +
|
| 81 | +try |
| 82 | +{ |
| 83 | + var done = new TaskCompletionSource<string>(); |
| 84 | + session.On(evt => |
| 85 | + { |
| 86 | + if (evt is AssistantMessageEvent msg) |
| 87 | + { |
| 88 | + done.SetResult(msg.Data.Content); |
| 89 | + } |
| 90 | + }); |
| 91 | +
|
| 92 | + await session.SendAsync(new MessageOptions { Prompt = "Complex question..." }); |
| 93 | +
|
| 94 | + // Wait with timeout (30 seconds) |
| 95 | + using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); |
| 96 | + var response = await done.Task.WaitAsync(cts.Token); |
| 97 | +
|
| 98 | + Console.WriteLine(response); |
| 99 | +} |
| 100 | +catch (OperationCanceledException) |
| 101 | +{ |
| 102 | + Console.WriteLine("Request timed out"); |
| 103 | +} |
| 104 | +``` |
| 105 | +
|
| 106 | +## Aborting a request |
| 107 | +
|
| 108 | +```csharp |
| 109 | +var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" }); |
| 110 | +
|
| 111 | +// Start a request |
| 112 | +await session.SendAsync(new MessageOptions { Prompt = "Write a very long story..." }); |
| 113 | +
|
| 114 | +// Abort it after some condition |
| 115 | +await Task.Delay(5000); |
| 116 | +await session.AbortAsync(); |
| 117 | +Console.WriteLine("Request aborted"); |
| 118 | +``` |
| 119 | +
|
| 120 | +## Graceful shutdown |
| 121 | +
|
| 122 | +```csharp |
| 123 | +Console.CancelKeyPress += async (sender, e) => |
| 124 | +{ |
| 125 | + e.Cancel = true; |
| 126 | + Console.WriteLine("Shutting down..."); |
| 127 | +
|
| 128 | + var errors = await client.StopAsync(); |
| 129 | + if (errors.Count > 0) |
| 130 | + { |
| 131 | + Console.WriteLine($"Cleanup errors: {string.Join(", ", errors)}"); |
| 132 | + } |
| 133 | +
|
| 134 | + Environment.Exit(0); |
| 135 | +}; |
| 136 | +``` |
| 137 | +
|
| 138 | +## Using await using for automatic disposal |
| 139 | +
|
| 140 | +```csharp |
| 141 | +await using var client = new CopilotClient(); |
| 142 | +await client.StartAsync(); |
| 143 | +
|
| 144 | +var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" }); |
| 145 | +
|
| 146 | +// ... do work ... |
| 147 | +
|
| 148 | +// client.StopAsync() is automatically called when exiting scope |
| 149 | +``` |
| 150 | +
|
| 151 | +## Best practices |
| 152 | +
|
| 153 | +1. **Always clean up**: Use try-finally or `await using` to ensure `StopAsync()` is called |
| 154 | +2. **Handle connection errors**: The CLI might not be installed or running |
| 155 | +3. **Set appropriate timeouts**: Use `CancellationToken` for long-running requests |
| 156 | +4. **Log errors**: Capture error details for debugging |
0 commit comments