From e5dc12a0c9d6c5323835a77795eddbf714089f10 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Thu, 29 Oct 2015 07:15:26 -0700 Subject: [PATCH] Add more context around some error cases Signed-off-by: Doug Davis --- libcontainer/cgroups/fs/utils.go | 11 ++++++++--- libcontainer/factory_linux.go | 11 ++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/libcontainer/cgroups/fs/utils.go b/libcontainer/cgroups/fs/utils.go index 3a6eec79..852b1839 100644 --- a/libcontainer/cgroups/fs/utils.go +++ b/libcontainer/cgroups/fs/utils.go @@ -44,7 +44,7 @@ func getCgroupParamKeyValue(t string) (string, uint64, error) { case 2: value, err := parseUint(parts[1], 10, 64) if err != nil { - return "", 0, fmt.Errorf("Unable to convert param value (%q) to uint64: %v", parts[1], err) + return "", 0, fmt.Errorf("unable to convert param value (%q) to uint64: %v", parts[1], err) } return parts[0], value, nil @@ -55,12 +55,17 @@ func getCgroupParamKeyValue(t string) (string, uint64, error) { // Gets a single uint64 value from the specified cgroup file. func getCgroupParamUint(cgroupPath, cgroupFile string) (uint64, error) { - contents, err := ioutil.ReadFile(filepath.Join(cgroupPath, cgroupFile)) + fileName := filepath.Join(cgroupPath, cgroupFile) + contents, err := ioutil.ReadFile(fileName) if err != nil { return 0, err } - return parseUint(strings.TrimSpace(string(contents)), 10, 64) + res, err := parseUint(strings.TrimSpace(string(contents)), 10, 64) + if err != nil { + return res, fmt.Errorf("unable to parse %q as a uint from Cgroup file %q", string(contents), fileName) + } + return res, nil } // Gets a string value from the specified cgroup file diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index 0f7ba678..70513f7b 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -159,7 +159,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err } containerRoot := filepath.Join(l.Root, id) if _, err := os.Stat(containerRoot); err == nil { - return nil, newGenericError(fmt.Errorf("Container with id exists: %v", id), IdInUse) + return nil, newGenericError(fmt.Errorf("container with id exists: %v", id), IdInUse) } else if !os.IsNotExist(err) { return nil, newGenericError(err, SystemError) } @@ -210,9 +210,10 @@ func (l *LinuxFactory) Type() string { // StartInitialization loads a container by opening the pipe fd from the parent to read the configuration and state // This is a low level implementation detail of the reexec and should not be consumed externally func (l *LinuxFactory) StartInitialization() (err error) { - pipefd, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_INITPIPE")) + fdStr := os.Getenv("_LIBCONTAINER_INITPIPE") + pipefd, err := strconv.Atoi(fdStr) if err != nil { - return err + return fmt.Errorf("error converting env var _LIBCONTAINER_INITPIPE(%q) to an int: %s", fdStr, err) } var ( pipe = os.NewFile(uintptr(pipefd), "pipe") @@ -260,10 +261,10 @@ func (l *LinuxFactory) loadState(root string) (*State, error) { func (l *LinuxFactory) validateID(id string) error { if !idRegex.MatchString(id) { - return newGenericError(fmt.Errorf("Invalid id format: %v", id), InvalidIdFormat) + return newGenericError(fmt.Errorf("invalid id format: %v", id), InvalidIdFormat) } if len(id) > maxIdLen { - return newGenericError(fmt.Errorf("Invalid id format: %v", id), InvalidIdFormat) + return newGenericError(fmt.Errorf("invalid id format: %v", id), InvalidIdFormat) } return nil }