From 2d6efc705109eb60dc6cd8a53ea00384db47f98e Mon Sep 17 00:00:00 2001 From: Christopher Obbard Date: Fri, 7 Jan 2022 11:17:12 +0000 Subject: [PATCH] Rework calls to panic into bubbled-up errors Rather than panicing, bubble up an error such that the execution may recover correctly. Signed-off-by: Christopher Obbard --- commands.go | 14 +++++++++----- filesystem.go | 7 +++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/commands.go b/commands.go index 42ba982d..cef60ec5 100644 --- a/commands.go +++ b/commands.go @@ -209,7 +209,11 @@ func (cmd *Command) restoreResolvConf(sum *[sha256.Size]byte) error { } func (cmd Command) Run(label string, cmdline ...string) error { - q := newQemuHelper(cmd) + q, err := newQemuHelper(cmd) + if err != nil { + return err + } + q.Setup() defer q.Cleanup() @@ -282,11 +286,11 @@ type qemuHelper struct { qemutarget string } -func newQemuHelper(c Command) qemuHelper { +func newQemuHelper(c Command) (qemuHelper, error) { q := qemuHelper{} if c.Chroot == "" || c.Architecture == "" { - return q + return q, nil } switch c.Architecture { @@ -305,14 +309,14 @@ func newQemuHelper(c Command) qemuHelper { case "amd64", "i386": /* Dummy, no qemu */ default: - log.Panicf("Don't know qemu for Architecture %s", c.Architecture) + return q, fmt.Errorf("Don't know qemu for architecture %s", c.Architecture) } if q.qemusrc != "" { q.qemutarget = path.Join(c.Chroot, q.qemusrc) } - return q + return q, nil } func (q qemuHelper) Setup() error { diff --git a/filesystem.go b/filesystem.go index b5c41fa9..ebefcc00 100644 --- a/filesystem.go +++ b/filesystem.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "io/ioutil" - "log" "os" "path" "path/filepath" @@ -71,18 +70,18 @@ func CopyTree(sourcetree, desttree string) error { case 0: err := CopyFile(p, target, info.Mode()) if err != nil { - log.Panicf("Failed to copy file %s: %v", p, err) + return fmt.Errorf("Failed to copy file %s: %v", p, err) } case os.ModeDir: os.Mkdir(target, info.Mode()) case os.ModeSymlink: link, err := os.Readlink(p) if err != nil { - log.Panicf("Failed to read symlink %s: %v", suffix, err) + return fmt.Errorf("Failed to read symlink %s: %v", suffix, err) } os.Symlink(link, target) default: - log.Panicf("Not handled /%s %v", suffix, info.Mode()) + return fmt.Errorf("Not handled /%s %v", suffix, info.Mode()) } return nil