Skip to content

Commit

Permalink
Merge pull request #107 from algorandfoundation/feat/improved-error-r…
Browse files Browse the repository at this point in the history
…eporting

fix: improve command error handling
  • Loading branch information
PhearZero authored Jan 21, 2025
2 parents 443b633 + a5c990d commit a808fe2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
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)
}
// Alert the User
return fmt.Errorf("%s: %s", style.Red.Render("Failed"), strings.Join(args, " "))
}

}
Expand Down

0 comments on commit a808fe2

Please sign in to comment.