Skip to content

Commit db81e4c

Browse files
authored
chore(internal/command): update error handling in runCmd (#4353)
This change modifies Librarian to use `errors.As` for checking exit errors during command execution. The `runCmd` function in `internal/command/command.go` is updated to replace a manual type assertion with `errors.As`. The error message is also refined to use the `%w` verb for error wrapping and includes the command's stderr for better debugging context.
1 parent 11de988 commit db81e4c

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

internal/command/command.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package command
1717

1818
import (
1919
"context"
20+
"errors"
2021
"fmt"
2122
"os"
2223
"os/exec"
@@ -78,10 +79,11 @@ func runCmd(ctx context.Context, dir string, env map[string]string, command stri
7879
}
7980
output, err := cmd.Output()
8081
if err != nil {
81-
if exitErr, ok := err.(*exec.ExitError); ok {
82-
return "", fmt.Errorf("%s: %v\n%s", cmd, err, exitErr.Stderr)
82+
var exitErr *exec.ExitError
83+
if errors.As(err, &exitErr) {
84+
return "", fmt.Errorf("%s: %s: %w", cmd, exitErr.Stderr, err)
8385
}
84-
return "", fmt.Errorf("%s: %v", cmd, err)
86+
return "", fmt.Errorf("%s: %w", cmd, err)
8587
}
8688
return string(output), nil
8789
}

0 commit comments

Comments
 (0)