|
5 | 5 | using System.Diagnostics;
|
6 | 6 | using System.IO;
|
7 | 7 | using System.Linq;
|
| 8 | +using System.Text; |
8 | 9 | using System.Text.Json;
|
9 | 10 | using System.Threading;
|
10 | 11 | using System.Threading.Tasks;
|
@@ -85,12 +86,15 @@ public List<Result> LoadContextMenus(Result selectedResult)
|
85 | 86 |
|
86 | 87 | private async Task<List<Result>> DeserializedResultAsync(Stream output)
|
87 | 88 | {
|
88 |
| - if (output == Stream.Null) return null; |
| 89 | + await using (output) |
| 90 | + { |
| 91 | + if (output == Stream.Null) return null; |
89 | 92 |
|
90 |
| - var queryResponseModel = |
91 |
| - await JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, options); |
| 93 | + var queryResponseModel = |
| 94 | + await JsonSerializer.DeserializeAsync<JsonRPCQueryResponseModel>(output, options); |
92 | 95 |
|
93 |
| - return ParseResults(queryResponseModel); |
| 96 | + return ParseResults(queryResponseModel); |
| 97 | + } |
94 | 98 | }
|
95 | 99 |
|
96 | 100 | private List<Result> DeserializedResult(string output)
|
@@ -132,7 +136,7 @@ private List<Result> ParseResults(JsonRPCQueryResponseModel queryResponseModel)
|
132 | 136 | }
|
133 | 137 | else
|
134 | 138 | {
|
135 |
| - var actionResponse = await RequestAsync(result.JsonRPCAction); |
| 139 | + await using var actionResponse = await RequestAsync(result.JsonRPCAction); |
136 | 140 |
|
137 | 141 | if (actionResponse.Length == 0)
|
138 | 142 | {
|
@@ -273,31 +277,18 @@ protected async Task<Stream> ExecuteAsync(ProcessStartInfo startInfo, Cancellati
|
273 | 277 | return Stream.Null;
|
274 | 278 | }
|
275 | 279 |
|
276 |
| - sourceBuffer.Seek(0, SeekOrigin.Begin); |
277 |
| - |
278 |
| - token.ThrowIfCancellationRequested(); |
279 |
| - |
280 |
| - if (sourceBuffer.Length == 0) |
281 |
| - { |
282 |
| - var errorMessage = errorBuffer.Length == 0 ? |
283 |
| - "Empty JSONRPC Response" : |
284 |
| - await process.StandardError.ReadToEndAsync(); |
285 |
| - |
286 |
| - Log.Error($"{context.CurrentPluginMetadata.Name}|{errorMessage}"); |
287 |
| - } |
288 |
| - |
289 |
| - if (errorBuffer.Length != 0) |
| 280 | + switch (sourceBuffer.Length, errorBuffer.Position) |
290 | 281 | {
|
291 |
| - using var error = new StreamReader(errorBuffer); |
292 |
| - |
293 |
| - var errorMessage = await error.ReadToEndAsync(); |
294 |
| - |
295 |
| - if (!string.IsNullOrEmpty(errorMessage)) |
296 |
| - { |
297 |
| - Log.Error($"|{context.CurrentPluginMetadata.Name}.{nameof(ExecuteAsync)}|{errorMessage}"); |
298 |
| - } |
| 282 | + case (0, 0): |
| 283 | + var errorMessage = Encoding.UTF8.GetString(errorBuffer.GetBuffer(), 0, (int)errorBuffer.Position); |
| 284 | + Log.Warn($"|{nameof(JsonRPCPlugin)}.{nameof(ExecuteAsync)}|{errorMessage}"); |
| 285 | + break; |
| 286 | + case (_, not 0): |
| 287 | + throw new InvalidDataException(Encoding.UTF8.GetString(errorBuffer.ToArray())); // The process has exited with an error message |
299 | 288 | }
|
300 | 289 |
|
| 290 | + sourceBuffer.Seek(0, SeekOrigin.Begin); |
| 291 | + |
301 | 292 | return sourceBuffer;
|
302 | 293 | }
|
303 | 294 |
|
|
0 commit comments