cmd: Properly wrap around errors

While we mostly report an error correctly, we do not include (wrap) the
underlying cause. This can make debugging tricky at times.

https://github.com/containers/toolbox/pull/728
This commit is contained in:
Ondřej Míchal 2021-03-20 14:47:50 +01:00
parent 8c1d441916
commit eedfdda535
5 changed files with 26 additions and 29 deletions

View file

@ -515,7 +515,7 @@ func getDBusSystemSocket() (string, error) {
path := addressSplit[1]
pathEvaled, err := filepath.EvalSymlinks(path)
if err != nil {
return "", errors.New("failed to resolve the path to the D-Bus system socket")
return "", fmt.Errorf("failed to resolve the path to the D-Bus system socket: %w", err)
}
return pathEvaled, nil
@ -583,7 +583,7 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
connection, err := dbus.SystemBus()
if err != nil {
return "", errors.New("failed to connect to the D-Bus system instance")
return "", fmt.Errorf("failed to connect to the D-Bus system instance: %w", err)
}
unitNameEscaped := systemdPathBusEscape(unitName)
@ -594,14 +594,12 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
var result map[string]dbus.Variant
err = call.Store(&result)
if err != nil {
errMsg := fmt.Sprintf("failed to get the properties of %s", unitName)
return "", errors.New(errMsg)
return "", fmt.Errorf("failed to get the properties of %s: %w", unitName, err)
}
listenVariant, listenFound := result["Listen"]
if !listenFound {
errMsg := fmt.Sprintf("failed to find the Listen property of %s", unitName)
return "", errors.New(errMsg)
return "", fmt.Errorf("failed to find the Listen property of %s: %w", unitName, err)
}
listenVariantSignature := listenVariant.Signature().String()
@ -627,8 +625,7 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
}
}
errMsg := fmt.Sprintf("failed to find a SOCK_STREAM socket for %s", unitName)
return "", errors.New(errMsg)
return "", fmt.Errorf("failed to find a SOCK_STREAM socket for %s", unitName)
}
func isPathReadWrite(path string) (bool, error) {

View file

@ -125,14 +125,14 @@ func listContainers() ([]toolboxContainer, error) {
args := []string{"--all", "--filter", "label=com.github.containers.toolbox=true"}
containers_old, err := podman.GetContainers(args...)
if err != nil {
return nil, errors.New("failed to list containers with label=com.github.containers.toolbox=true")
return nil, fmt.Errorf("failed to list containers with label=com.github.containers.toolbox=true: %w", err)
}
logrus.Debug("Fetching containers with label=com.github.debarshiray.toolbox=true")
args = []string{"--all", "--filter", "label=com.github.debarshiray.toolbox=true"}
containers_new, err := podman.GetContainers(args...)
if err != nil {
return nil, errors.New("failed to list containers with label=com.github.debarshiray.toolbox=true")
return nil, fmt.Errorf("failed to list containers with label=com.github.debarshiray.toolbox=true: %w", err)
}
var containers []map[string]interface{}

View file

@ -74,14 +74,14 @@ func rm(cmd *cobra.Command, args []string) error {
args := []string{"--all", "--filter", "label=com.github.containers.toolbox=true"}
containers_old, err := podman.GetContainers(args...)
if err != nil {
return errors.New("failed to list containers with label=com.github.containers.toolbox=true")
return fmt.Errorf("failed to list containers with label=com.github.containers.toolbox=true: %w", err)
}
logrus.Debug("Fetching containers with label=com.github.debarshiray.toolbox=true")
args = []string{"--all", "--filter", "label=com.github.debarshiray.toolbox=true"}
containers_new, err := podman.GetContainers(args...)
if err != nil {
return errors.New("failed to list containers with com.github.debarshiray.toolbox=true")
return fmt.Errorf("failed to list containers with com.github.debarshiray.toolbox=true: %w", err)
}
var idKey string

View file

@ -74,14 +74,14 @@ func rmi(cmd *cobra.Command, args []string) error {
args := []string{"--filter", "label=com.github.containers.toolbox=true"}
images_old, err := podman.GetImages(args...)
if err != nil {
return errors.New("failed to list images with label=com.github.containers.toolbox=true")
return fmt.Errorf("failed to list images with label=com.github.containers.toolbox=true: %w", err)
}
logrus.Debug("Fetching images with label=com.github.debarshiray.toolbox=true")
args = []string{"--filter", "label=com.github.debarshiray.toolbox=true"}
images_new, err := podman.GetImages(args...)
if err != nil {
return errors.New("failed to list images with com.github.debarshiray.toolbox=true")
return fmt.Errorf("failed to list images with com.github.debarshiray.toolbox=true: %w", err)
}
var idKey string

View file

@ -205,7 +205,7 @@ func migrate() error {
configDir, err := os.UserConfigDir()
if err != nil {
return fmt.Errorf("failed to get the user config directory")
return fmt.Errorf("failed to get the user config directory: %w", err)
}
toolboxConfigDir := configDir + "/toolbox"
@ -214,14 +214,14 @@ func migrate() error {
podmanVersion, err := podman.GetVersion()
if err != nil {
return fmt.Errorf("failed to get the Podman version")
return fmt.Errorf("failed to get the Podman version: %w", err)
}
logrus.Debugf("Current Podman version is %s", podmanVersion)
err = os.MkdirAll(toolboxConfigDir, 0775)
if err != nil {
return fmt.Errorf("failed to create configuration directory")
return fmt.Errorf("failed to create configuration directory: %w", err)
}
toolboxRuntimeDirectory, err := utils.GetRuntimeDirectory(currentUser)
@ -233,7 +233,7 @@ func migrate() error {
migrateLockFile, err := os.Create(migrateLock)
if err != nil {
return fmt.Errorf("failed to create migration lock file")
return fmt.Errorf("failed to create migration lock file: %w", err)
}
defer migrateLockFile.Close()
@ -241,13 +241,13 @@ func migrate() error {
migrateLockFD := migrateLockFile.Fd()
migrateLockFDInt := int(migrateLockFD)
if err := syscall.Flock(migrateLockFDInt, syscall.LOCK_EX); err != nil {
return fmt.Errorf("failed to acquire migration lock")
return fmt.Errorf("failed to acquire migration lock: %w", err)
}
stampBytes, err := ioutil.ReadFile(stampPath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("failed to read migration stamp file")
return fmt.Errorf("failed to read migration stamp file: %w", err)
}
} else {
stampString := string(stampBytes)
@ -270,7 +270,7 @@ func migrate() error {
}
if err = podman.SystemMigrate(""); err != nil {
return fmt.Errorf("failed to migrate containers")
return fmt.Errorf("failed to migrate containers: %w", err)
}
logrus.Debugf("Migration to Podman version %s was ok", podmanVersion)
@ -279,7 +279,7 @@ func migrate() error {
podmanVersionBytes := []byte(podmanVersion + "\n")
err = ioutil.WriteFile(stampPath, podmanVersionBytes, 0664)
if err != nil {
return fmt.Errorf("failed to update Podman version in migration stamp file")
return fmt.Errorf("failed to update Podman version in migration stamp file: %w", err)
}
return nil
@ -301,30 +301,30 @@ func setUpGlobals() error {
if !utils.IsInsideContainer() {
cgroupsVersion, err = utils.GetCgroupsVersion()
if err != nil {
return errors.New("failed to get the cgroups version")
return fmt.Errorf("failed to get the cgroups version: %w", err)
}
}
currentUser, err = user.Current()
if err != nil {
return errors.New("failed to get the current user")
return fmt.Errorf("failed to get the current user: %w", err)
}
executable, err = os.Executable()
if err != nil {
return errors.New("failed to get the path to the executable")
return fmt.Errorf("failed to get the path to the executable: %w", err)
}
executable, err = filepath.EvalSymlinks(executable)
if err != nil {
return errors.New("failed to resolve absolute path to the executable")
return fmt.Errorf("failed to resolve absolute path to the executable: %w", err)
}
executableBase = filepath.Base(executable)
workingDirectory, err = os.Getwd()
if err != nil {
return errors.New("failed to get the working directory")
return fmt.Errorf("failed to get the working directory: %w", err)
}
return nil
@ -342,7 +342,7 @@ func setUpLoggers() error {
logLevel, err := logrus.ParseLevel(rootFlags.logLevel)
if err != nil {
return errors.New("failed to parse log-level")
return fmt.Errorf("failed to parse log-level: %w", err)
}
logrus.SetLevel(logLevel)
@ -361,7 +361,7 @@ func setUpLoggers() error {
func validateSubIDFile(path string) (bool, error) {
file, err := os.Open(path)
if err != nil {
return false, fmt.Errorf("failed to open %s", path)
return false, fmt.Errorf("failed to open %s: %w", path, err)
}
scanner := bufio.NewScanner(file)