Skip to content

Commit

Permalink
Rework calls to panic into bubbled-up errors
Browse files Browse the repository at this point in the history
Rather than panicing, bubble up an error such that
the execution may recover correctly.

Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc committed Jan 7, 2022
1 parent a95ed84 commit f640c00
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 9 additions & 5 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand All @@ -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 nil, errors.New("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 {
Expand Down
7 changes: 3 additions & 4 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f640c00

Please sign in to comment.