Skip to content

Commit 26cae57

Browse files
committed
Show user-friendly error when dotnet tool requires Windows admin elevation
1 parent 1b95805 commit 26cae57

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

internal/capture/dotnet_utils.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@ import (
1010
"yc-agent/internal/logger"
1111
)
1212

13+
// dotnetToolFriendlyError defines a mapping from an error substring to a
14+
// user-friendly message that helps the operator resolve the problem.
15+
type dotnetToolFriendlyError struct {
16+
substring string
17+
message string
18+
}
19+
20+
// knownDotnetToolErrors lists recognised error patterns and the guidance
21+
// that should be shown to the user alongside the original error message.
22+
var knownDotnetToolErrors = []dotnetToolFriendlyError{
23+
{
24+
substring: "requires elevation",
25+
message: "administrator privileges are required. Please re-run the command from an elevated Command Prompt or PowerShell (Run as Administrator)",
26+
},
27+
}
28+
29+
// wrapDotnetToolStartError wraps a command-start error, appending a
30+
// user-friendly message when the error matches a known pattern. The original
31+
// error message is always preserved for debugging.
32+
func wrapDotnetToolStartError(err error, cmdArgs []string) error {
33+
msg := err.Error()
34+
for _, known := range knownDotnetToolErrors {
35+
if strings.Contains(msg, known.substring) {
36+
return fmt.Errorf("failed to start dotnet tool %v: %s\nOriginal error: %w", cmdArgs, known.message, err)
37+
}
38+
}
39+
return fmt.Errorf("failed to start dotnet tool %v: %w", cmdArgs, err)
40+
}
41+
1342
// ensureDotnetToolResolved lazily resolves DotnetToolPath if it was not set
1443
// during validation (e.g. when runtime was auto-detected rather than explicit).
1544
func ensureDotnetToolResolved() (string, error) {
@@ -40,7 +69,7 @@ func executeDotnetTool(args []string, outputPath string) (*os.File, error) {
4069
// Execute the command and capture output to file
4170
cmd, err := executils.CommandStartInBackground(cmdArgs)
4271
if err != nil {
43-
return nil, fmt.Errorf("failed to start dotnet tool %v: %w", cmdArgs, err)
72+
return nil, wrapDotnetToolStartError(err, cmdArgs)
4473
}
4574

4675
// Wait for command to complete
@@ -128,7 +157,7 @@ func startDotnetToolInBackground(args []string) (executils.CmdManager, error) {
128157

129158
cmd, err := executils.CommandStartInBackground(cmdArgs)
130159
if err != nil {
131-
return nil, fmt.Errorf("failed to start dotnet tool %v: %w", cmdArgs, err)
160+
return nil, wrapDotnetToolStartError(err, cmdArgs)
132161
}
133162

134163
return cmd, nil

0 commit comments

Comments
 (0)