Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve command error handling #107

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"fmt"
"github.com/algorandfoundation/nodekit/api"
"github.com/algorandfoundation/nodekit/cmd/utils/explanations"
"github.com/algorandfoundation/nodekit/internal/algod"
Expand All @@ -20,8 +21,7 @@ func WithInvalidResponsesExplanations(err error, response api.ResponseInterface,
}
if response.StatusCode() > 300 {
log.Fatal(
style.Red.Render("failed to get status: error code %d")+"\n\n"+explanations.TokenNotAdmin+"\n"+postFix,
response.StatusCode())
style.Red.Render(fmt.Sprintf("failed to get status: error code %d", response.StatusCode())) + "\n\n" + explanations.TokenNotAdmin + "\n" + postFix)
}
}

Expand Down
23 changes: 18 additions & 5 deletions internal/system/cmds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package system

import (
"errors"
"fmt"
"github.com/algorandfoundation/nodekit/ui/style"
"github.com/charmbracelet/log"
Expand All @@ -11,9 +12,6 @@ import (
"sync"
)

// CmdFailedErrorMsg is a formatted error message used to detail command failures, including output and the associated error.
const CmdFailedErrorMsg = "command failed: %s output: %s error: %v"

// IsSudo checks if the process is running with root privileges by verifying the effective user ID is 0.
func IsSudo() bool {
return os.Geteuid() == 0
Expand Down Expand Up @@ -60,9 +58,24 @@ func RunAll(list CmdsList) error {
log.Debug(fmt.Sprintf("%s: %s", style.Green.Render("Running"), strings.Join(args, " ")))
cmd := exec.Command(args[0], args[1:]...)
output, err := cmd.CombinedOutput()

if err != nil {
log.Error(fmt.Sprintf("%s: %s", style.Red.Render("Failed"), strings.Join(args, " ")))
return fmt.Errorf(CmdFailedErrorMsg, strings.Join(args, " "), output, err)
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
// Get Stderr when possible
if exitErr.Stderr != nil {
log.Error(exitErr.Stderr)
} else {
// Report the output
log.Error(strings.TrimSuffix(string(output), "\n"))
}

} else {
// Log the regular errors
log.Error(err)
}
tasosbit marked this conversation as resolved.
Show resolved Hide resolved
// Alert the User
return fmt.Errorf("%s: %s", style.Red.Render("Failed"), strings.Join(args, " "))
}

}
Expand Down
Loading