Skip to content

Commit a959c5d

Browse files
Copilotjongalloway
andcommitted
Fix code review issues: Remove dynamic keyword and improve error handling
1. DotNetResources.cs: Replace dynamic keyword with reflection-based property access. Use indexed access instead of Last() to avoid potential exceptions. 2. DotNetCommandExecutor.cs: Improve error handling to throw exceptions on any non-zero exit code, not just when stderr has content. Provides clear error messages for both cases. Co-authored-by: jongalloway <[email protected]>
1 parent 5bc0fb0 commit a959c5d

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

DotNetMcp/DotNetCommandExecutor.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,13 @@ public static async Task<string> ExecuteCommandForResourceAsync(string arguments
132132
var error = await process.StandardError.ReadToEndAsync();
133133
await process.WaitForExitAsync();
134134

135-
if (process.ExitCode != 0 && !string.IsNullOrEmpty(error))
135+
if (process.ExitCode != 0)
136136
{
137137
logger?.LogError("Command failed with exit code {ExitCode}: {Error}", process.ExitCode, error);
138-
throw new InvalidOperationException($"dotnet command failed: {error}");
138+
var errorMessage = !string.IsNullOrEmpty(error)
139+
? $"dotnet command failed: {error}"
140+
: $"dotnet command failed with exit code {process.ExitCode} and no error output.";
141+
throw new InvalidOperationException(errorMessage);
139142
}
140143

141144
logger?.LogDebug("Command completed successfully");

DotNetMcp/DotNetResources.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ public async Task<string> GetSdkInfo()
4646
}
4747
}
4848

49+
var lastSdk = sdks.Count > 0 ? sdks[sdks.Count - 1] : null;
4950
var response = new
5051
{
5152
sdks,
52-
latestSdk = sdks.LastOrDefault() != null ? ((dynamic)sdks.Last()).version : null
53+
latestSdk = lastSdk?.GetType().GetProperty("version")?.GetValue(lastSdk) as string
5354
};
5455

5556
return JsonSerializer.Serialize(response, new JsonSerializerOptions { WriteIndented = true });

0 commit comments

Comments
 (0)