refactor to reduce duplicated error parsing
Signed-off-by: Peter Hunt <pehunt@redhat.com>
This commit is contained in:
parent
82dce36fb6
commit
01a8483a59
|
|
@ -1,5 +1,9 @@
|
|||
package define
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
// ExecErrorCodeGeneric is the default error code to return from an exec session if libpod failed
|
||||
// prior to calling the runtime
|
||||
|
|
@ -11,3 +15,16 @@ const (
|
|||
// ExecErrorCodeNotFound is the error code to return when a command cannot be found
|
||||
ExecErrorCodeNotFound = 127
|
||||
)
|
||||
|
||||
// TranslateExecErrorToExitCode takes an error and checks whether it
|
||||
// has a predefined exit code associated. If so, it returns that, otherwise it returns
|
||||
// the exit code originally stated in libpod.Exec()
|
||||
func TranslateExecErrorToExitCode(originalEC int, err error) int {
|
||||
if errors.Cause(err) == ErrOCIRuntimePermissionDenied {
|
||||
return ExecErrorCodeCannotInvoke
|
||||
}
|
||||
if errors.Cause(err) == ErrOCIRuntimeNotFound {
|
||||
return ExecErrorCodeNotFound
|
||||
}
|
||||
return originalEC
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1001,13 +1001,7 @@ func (r *LocalRuntime) ExecContainer(ctx context.Context, cli *cliconfig.ExecVal
|
|||
streams.AttachError = true
|
||||
|
||||
ec, err = ExecAttachCtr(ctx, ctr.Container, cli.Tty, cli.Privileged, envs, cmd, cli.User, cli.Workdir, streams, cli.PreserveFDs, cli.DetachKeys)
|
||||
if errors.Cause(err) == define.ErrOCIRuntimePermissionDenied {
|
||||
ec = 126
|
||||
}
|
||||
if errors.Cause(err) == define.ErrOCIRuntimeNotFound {
|
||||
ec = 127
|
||||
}
|
||||
return ec, err
|
||||
return define.TranslateExecErrorToExitCode(ec, err), err
|
||||
}
|
||||
|
||||
// Prune removes stopped containers
|
||||
|
|
|
|||
|
|
@ -837,15 +837,9 @@ func (i *LibpodAPI) ExecContainer(call iopodman.VarlinkCall, opts iopodman.ExecO
|
|||
|
||||
ecErr := <-ecErrChan
|
||||
|
||||
exitCode := ecErr.ExitCode
|
||||
if errors.Cause(ecErr.Error) == define.ErrOCIRuntimePermissionDenied {
|
||||
exitCode = define.ExecErrorCodeCannotInvoke
|
||||
}
|
||||
if errors.Cause(ecErr.Error) == define.ErrOCIRuntimeNotFound {
|
||||
exitCode = define.ExecErrorCodeNotFound
|
||||
}
|
||||
exitCode := define.TranslateExecErrorToExitCode(int(ecErr.ExitCode), ecErr.Error)
|
||||
|
||||
if err = virtwriter.HangUp(writer, exitCode); err != nil {
|
||||
if err = virtwriter.HangUp(writer, uint32(exitCode)); err != nil {
|
||||
logrus.Errorf("ExecContainer failed to HANG-UP on %s: %s", ctr.ID(), err.Error())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue