[lxc-devel] [distrobuilder/master] *: Use errors.Wrap() when possible

monstermunchkin on Github lxc-bot at linuxcontainers.org
Thu Feb 20 09:22:07 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 364 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200220/c50b28fc/attachment.bin>
-------------- next part --------------
From 90c2c4751f98a933ed4cb7efbb16bd100d8a914d Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 20 Feb 2020 10:20:46 +0100
Subject: [PATCH] *: Use errors.Wrap() when possible

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 distrobuilder/main_lxc.go | 10 +++++-----
 distrobuilder/main_lxd.go | 10 +++++-----
 shared/net.go             |  3 ++-
 shared/util.go            | 11 ++++++-----
 sources/centos-http.go    |  4 ++--
 5 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/distrobuilder/main_lxc.go b/distrobuilder/main_lxc.go
index 2ca3b98f..950a3cbd 100644
--- a/distrobuilder/main_lxc.go
+++ b/distrobuilder/main_lxc.go
@@ -65,7 +65,7 @@ func (c *cmdLXC) runPack(cmd *cobra.Command, args []string, overlayDir string) e
 	// Setup the mounts and chroot into the rootfs
 	exitChroot, err := shared.SetupChroot(overlayDir, c.global.definition.Environment, nil)
 	if err != nil {
-		return fmt.Errorf("Failed to setup chroot: %s", err)
+		return errors.Wrap(err, "Failed to setup chroot")
 	}
 	// Unmount everything and exit the chroot
 	defer exitChroot()
@@ -84,28 +84,28 @@ func (c *cmdLXC) runPack(cmd *cobra.Command, args []string, overlayDir string) e
 
 	err = manageRepositories(c.global.definition, manager, imageTargets)
 	if err != nil {
-		return fmt.Errorf("Failed to manage repositories: %s", err)
+		return errors.Wrap(err, "Failed to manage repositories")
 	}
 
 	// Run post unpack hook
 	for _, hook := range c.global.definition.GetRunnableActions("post-unpack", imageTargets) {
 		err := shared.RunScript(hook.Action)
 		if err != nil {
-			return fmt.Errorf("Failed to run post-unpack: %s", err)
+			return errors.Wrap(err, "Failed to run post-unpack")
 		}
 	}
 
 	// Install/remove/update packages
 	err = managePackages(c.global.definition, manager, imageTargets)
 	if err != nil {
-		return fmt.Errorf("Failed to manage packages: %s", err)
+		return errors.Wrap(err, "Failed to manage packages")
 	}
 
 	// Run post packages hook
 	for _, hook := range c.global.definition.GetRunnableActions("post-packages", imageTargets) {
 		err := shared.RunScript(hook.Action)
 		if err != nil {
-			return fmt.Errorf("Failed to run post-packages: %s", err)
+			return errors.Wrap(err, "Failed to run post-packages")
 		}
 	}
 
diff --git a/distrobuilder/main_lxd.go b/distrobuilder/main_lxd.go
index 1915ec27..828eb2fc 100644
--- a/distrobuilder/main_lxd.go
+++ b/distrobuilder/main_lxd.go
@@ -119,7 +119,7 @@ func (c *cmdLXD) runPack(cmd *cobra.Command, args []string, overlayDir string) e
 	// Setup the mounts and chroot into the rootfs
 	exitChroot, err := shared.SetupChroot(overlayDir, c.global.definition.Environment, nil)
 	if err != nil {
-		return fmt.Errorf("Failed to setup chroot: %s", err)
+		return errors.Wrapf(err, "Failed to setup chroot")
 	}
 	// Unmount everything and exit the chroot
 	defer exitChroot()
@@ -144,28 +144,28 @@ func (c *cmdLXD) runPack(cmd *cobra.Command, args []string, overlayDir string) e
 
 	err = manageRepositories(c.global.definition, manager, imageTargets)
 	if err != nil {
-		return fmt.Errorf("Failed to manage repositories: %s", err)
+		return errors.Wrap(err, "Failed to manage repositories")
 	}
 
 	// Run post unpack hook
 	for _, hook := range c.global.definition.GetRunnableActions("post-unpack", imageTargets) {
 		err := shared.RunScript(hook.Action)
 		if err != nil {
-			return fmt.Errorf("Failed to run post-unpack: %s", err)
+			return errors.Wrap(err, "Failed to run post-unpack")
 		}
 	}
 
 	// Install/remove/update packages
 	err = managePackages(c.global.definition, manager, imageTargets)
 	if err != nil {
-		return fmt.Errorf("Failed to manage packages: %s", err)
+		return errors.Wrap(err, "Failed to manage packages")
 	}
 
 	// Run post packages hook
 	for _, hook := range c.global.definition.GetRunnableActions("post-packages", imageTargets) {
 		err := shared.RunScript(hook.Action)
 		if err != nil {
-			return fmt.Errorf("Failed to run post-packages: %s", err)
+			return errors.Wrap(err, "Failed to run post-packages")
 		}
 	}
 
diff --git a/shared/net.go b/shared/net.go
index 993edf77..408425e5 100644
--- a/shared/net.go
+++ b/shared/net.go
@@ -12,6 +12,7 @@ import (
 
 	lxd "github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/ioprogress"
+	"github.com/pkg/errors"
 )
 
 // DownloadHash downloads a file. If a checksum file is provided, it will try and
@@ -41,7 +42,7 @@ func DownloadHash(def DefinitionImage, file, checksum string, hashFunc hash.Hash
 
 		hash, err = downloadChecksum(targetDir, checksum, file, hashFunc, hashLen)
 		if err != nil {
-			return "", fmt.Errorf("Error while downloading checksum: %s", err)
+			return "", errors.Wrap(err, "Error while downloading checksum")
 		}
 	}
 
diff --git a/shared/util.go b/shared/util.go
index 04a3937b..b26f0423 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -16,6 +16,7 @@ import (
 	"time"
 
 	lxd "github.com/lxc/lxd/shared"
+	"github.com/pkg/errors"
 	"gopkg.in/flosch/pongo2.v3"
 	yaml "gopkg.in/yaml.v2"
 )
@@ -92,7 +93,7 @@ func GetSignedContent(signedFile string, keys []string, keyserver string) ([]byt
 	out, err := exec.Command("gpg", "--homedir", gpgDir, "--keyring", keyring,
 		"--decrypt", signedFile).Output()
 	if err != nil {
-		return nil, fmt.Errorf("Failed to get file content: %v", err)
+		return nil, errors.Wrapf(err, "Failed to get file content: %s", out)
 	}
 
 	return out, nil
@@ -111,13 +112,13 @@ func VerifyFile(signedFile, signatureFile string, keys []string, keyserver strin
 		out, err := lxd.RunCommand("gpg", "--homedir", gpgDir, "--keyring", keyring,
 			"--verify", signatureFile, signedFile)
 		if err != nil {
-			return false, fmt.Errorf("Failed to verify: %s", out)
+			return false, errors.Wrapf(err, "Failed to verify: %s", out)
 		}
 	} else {
 		out, err := lxd.RunCommand("gpg", "--homedir", gpgDir, "--keyring", keyring,
 			"--verify", signedFile)
 		if err != nil {
-			return false, fmt.Errorf("Failed to verify: %s", out)
+			return false, errors.Wrapf(err, "Failed to verify: %s", out)
 		}
 	}
 
@@ -196,7 +197,7 @@ func recvGPGKeys(gpgDir string, keyserver string, keys []string) (bool, error) {
 func CreateGPGKeyring(keyserver string, keys []string) (string, error) {
 	gpgDir, err := ioutil.TempDir(os.TempDir(), "distrobuilder.")
 	if err != nil {
-		return "", fmt.Errorf("Failed to create gpg directory: %s", err)
+		return "", errors.Wrap(err, "Failed to create gpg directory")
 	}
 
 	err = os.MkdirAll(gpgDir, 0700)
@@ -224,7 +225,7 @@ func CreateGPGKeyring(keyserver string, keys []string) (string, error) {
 		filepath.Join(gpgDir, "distrobuilder.gpg"))
 	if err != nil {
 		os.RemoveAll(gpgDir)
-		return "", fmt.Errorf("Failed to export keyring: %s", out)
+		return "", errors.Wrapf(err, "Failed to export keyring: %s", out)
 	}
 
 	return filepath.Join(gpgDir, "distrobuilder.gpg"), nil
diff --git a/sources/centos-http.go b/sources/centos-http.go
index a2cb3d20..3870c1f1 100644
--- a/sources/centos-http.go
+++ b/sources/centos-http.go
@@ -171,7 +171,7 @@ func (s CentOSHTTP) unpackRaw(filePath, rootfsDir string) error {
 	// Setup the mounts and chroot into the rootfs
 	exitChroot, err := shared.SetupChroot(tempRootDir, shared.DefinitionEnv{}, nil)
 	if err != nil {
-		return fmt.Errorf("Failed to setup chroot: %s", err)
+		return errors.Wrap(err, "Failed to setup chroot")
 	}
 
 	err = shared.RunScript(fmt.Sprintf(`
@@ -298,7 +298,7 @@ func (s CentOSHTTP) unpackISO(filePath, rootfsDir string) error {
 	// Setup the mounts and chroot into the rootfs
 	exitChroot, err := shared.SetupChroot(tempRootDir, shared.DefinitionEnv{}, nil)
 	if err != nil {
-		return fmt.Errorf("Failed to setup chroot: %s", err)
+		return errors.Wrap(err, "Failed to setup chroot")
 	}
 
 	err = shared.RunScript(fmt.Sprintf(`


More information about the lxc-devel mailing list