Skip to content

Commit 81d8dec

Browse files
Only wrap RPC invocation calls in try-catch blocks
Co-authored-by: SteveSandersonMS <1101362+SteveSandersonMS@users.noreply.github.com>
1 parent 8598f71 commit 81d8dec

2 files changed

Lines changed: 23 additions & 12 deletions

File tree

dotnet/src/Client.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,15 +477,18 @@ public async Task<PingResponse> PingAsync(string? message = null, CancellationTo
477477
{
478478
var connection = await EnsureConnectedAsync(cancellationToken);
479479

480+
PingResponse response;
480481
try
481482
{
482-
return await connection.Rpc.InvokeWithCancellationAsync<PingResponse>(
483+
response = await connection.Rpc.InvokeWithCancellationAsync<PingResponse>(
483484
"ping", [new PingRequest { Message = message }], cancellationToken);
484485
}
485486
catch (StreamJsonRpc.RemoteInvocationException ex)
486487
{
487488
throw new CopilotRpcException($"Failed to ping server: {ex.Message}", ex);
488489
}
490+
491+
return response;
489492
}
490493

491494
/// <summary>
@@ -498,15 +501,18 @@ public async Task<GetStatusResponse> GetStatusAsync(CancellationToken cancellati
498501
{
499502
var connection = await EnsureConnectedAsync(cancellationToken);
500503

504+
GetStatusResponse response;
501505
try
502506
{
503-
return await connection.Rpc.InvokeWithCancellationAsync<GetStatusResponse>(
507+
response = await connection.Rpc.InvokeWithCancellationAsync<GetStatusResponse>(
504508
"status.get", [], cancellationToken);
505509
}
506510
catch (StreamJsonRpc.RemoteInvocationException ex)
507511
{
508512
throw new CopilotRpcException($"Failed to get status: {ex.Message}", ex);
509513
}
514+
515+
return response;
510516
}
511517

512518
/// <summary>
@@ -519,15 +525,18 @@ public async Task<GetAuthStatusResponse> GetAuthStatusAsync(CancellationToken ca
519525
{
520526
var connection = await EnsureConnectedAsync(cancellationToken);
521527

528+
GetAuthStatusResponse response;
522529
try
523530
{
524-
return await connection.Rpc.InvokeWithCancellationAsync<GetAuthStatusResponse>(
531+
response = await connection.Rpc.InvokeWithCancellationAsync<GetAuthStatusResponse>(
525532
"auth.getStatus", [], cancellationToken);
526533
}
527534
catch (StreamJsonRpc.RemoteInvocationException ex)
528535
{
529536
throw new CopilotRpcException($"Failed to get auth status: {ex.Message}", ex);
530537
}
538+
539+
return response;
531540
}
532541

533542
/// <summary>

dotnet/src/Session.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ public async Task<string> SendAsync(MessageOptions options, CancellationToken ca
118118
Mode = options.Mode
119119
};
120120

121+
SendMessageResponse response;
121122
try
122123
{
123-
var response = await _rpc.InvokeWithCancellationAsync<SendMessageResponse>(
124+
response = await _rpc.InvokeWithCancellationAsync<SendMessageResponse>(
124125
"session.send", [request], cancellationToken);
125-
126-
return response.MessageId;
127126
}
128127
catch (StreamJsonRpc.RemoteInvocationException ex)
129128
{
130129
throw new CopilotRpcException($"Failed to send message: {ex.Message}", ex);
131130
}
131+
132+
return response.MessageId;
132133
}
133134

134135
/// <summary>
@@ -358,20 +359,21 @@ internal async Task<PermissionRequestResult> HandlePermissionRequestAsync(JsonEl
358359
/// </example>
359360
public async Task<IReadOnlyList<SessionEvent>> GetMessagesAsync(CancellationToken cancellationToken = default)
360361
{
362+
GetMessagesResponse response;
361363
try
362364
{
363-
var response = await _rpc.InvokeWithCancellationAsync<GetMessagesResponse>(
365+
response = await _rpc.InvokeWithCancellationAsync<GetMessagesResponse>(
364366
"session.getMessages", [new GetMessagesRequest { SessionId = SessionId }], cancellationToken);
365-
366-
return response.Events
367-
.Select(e => SessionEvent.FromJson(e.ToJsonString()))
368-
.OfType<SessionEvent>()
369-
.ToList();
370367
}
371368
catch (StreamJsonRpc.RemoteInvocationException ex)
372369
{
373370
throw new CopilotRpcException($"Failed to get messages: {ex.Message}", ex);
374371
}
372+
373+
return response.Events
374+
.Select(e => SessionEvent.FromJson(e.ToJsonString()))
375+
.OfType<SessionEvent>()
376+
.ToList();
375377
}
376378

377379
/// <summary>

0 commit comments

Comments
 (0)