From 4ce8d973204ebace2970c662f6f841ab11a3cc13 Mon Sep 17 00:00:00 2001 From: "Daniel, Dao Quang Minh" Date: Fri, 27 Feb 2015 03:47:57 -0500 Subject: [PATCH 1/2] do not pass nil to genericError currently genericError constructors require not-nil error to be able to read its Error() message Signed-off-by: Daniel, Dao Quang Minh --- container_linux.go | 2 +- process.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/container_linux.go b/container_linux.go index 3ea08db26..8fffd39d1 100644 --- a/container_linux.go +++ b/container_linux.go @@ -206,7 +206,7 @@ func (c *linuxContainer) Destroy() error { return err } if status != Destroyed { - return newGenericError(nil, ContainerNotStopped) + return newGenericError(fmt.Errorf("container is not destroyed"), ContainerNotStopped) } if !c.config.Namespaces.Contains(configs.NEWPID) { if err := killCgroupProcesses(c.cgroupManager); err != nil { diff --git a/process.go b/process.go index 8ae741639..1f769abb4 100644 --- a/process.go +++ b/process.go @@ -1,6 +1,7 @@ package libcontainer import ( + "fmt" "io" "os" ) @@ -46,7 +47,7 @@ type Process struct { // Wait releases any resources associated with the Process func (p Process) Wait() (*os.ProcessState, error) { if p.ops == nil { - return nil, newGenericError(nil, ProcessNotExecuted) + return nil, newGenericError(fmt.Errorf("invalid process"), ProcessNotExecuted) } return p.ops.wait() } @@ -54,7 +55,7 @@ func (p Process) Wait() (*os.ProcessState, error) { // Pid returns the process ID func (p Process) Pid() (int, error) { if p.ops == nil { - return -1, newGenericError(nil, ProcessNotExecuted) + return -1, newGenericError(fmt.Errorf("invalid process"), ProcessNotExecuted) } return p.ops.pid(), nil } @@ -62,7 +63,7 @@ func (p Process) Pid() (int, error) { // Signal sends a signal to the Process. func (p Process) Signal(sig os.Signal) error { if p.ops == nil { - return newGenericError(nil, ProcessNotExecuted) + return newGenericError(fmt.Errorf("invalid process"), ProcessNotExecuted) } return p.ops.signal(sig) } From e22b58954324b3593737438032412f15ed9602e9 Mon Sep 17 00:00:00 2001 From: "Daniel, Dao Quang Minh" Date: Fri, 27 Feb 2015 03:50:20 -0500 Subject: [PATCH 2/2] genericError constructors can accept nil error if the error is nil, we do not populate generic error's message, but the constructor will still return a valid error Signed-off-by: Daniel, Dao Quang Minh --- generic_error.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/generic_error.go b/generic_error.go index 05ab0a9d3..ff4d7248d 100644 --- a/generic_error.go +++ b/generic_error.go @@ -25,26 +25,32 @@ func newGenericError(err error, c ErrorCode) Error { if le, ok := err.(Error); ok { return le } - return &genericError{ + gerr := &genericError{ Timestamp: time.Now(), Err: err, - Message: err.Error(), ECode: c, Stack: stacktrace.Capture(1), } + if err != nil { + gerr.Message = err.Error() + } + return gerr } func newSystemError(err error) Error { if le, ok := err.(Error); ok { return le } - return &genericError{ + gerr := &genericError{ Timestamp: time.Now(), Err: err, ECode: SystemError, - Message: err.Error(), Stack: stacktrace.Capture(1), } + if err != nil { + gerr.Message = err.Error() + } + return gerr } type genericError struct {