remote run: fix error checks

As error types are not preserved on the client side (due to marshaling),
we cannot use `errors.Cause(...)` and friends but, unfortunately, have
to fall back to looking for substring the error messages.

Change the error checks in remote run to do substring matches and fix
issue #7340.

Fixes: #7340
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2020-09-11 11:56:18 +02:00
parent 2a637948e7
commit 204493173e
2 changed files with 12 additions and 3 deletions

View File

@ -19,6 +19,7 @@ import (
"github.com/containers/podman/v2/pkg/bindings" "github.com/containers/podman/v2/pkg/bindings"
"github.com/containers/podman/v2/pkg/bindings/containers" "github.com/containers/podman/v2/pkg/bindings/containers"
"github.com/containers/podman/v2/pkg/domain/entities" "github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/errorhandling"
"github.com/containers/podman/v2/pkg/specgen" "github.com/containers/podman/v2/pkg/specgen"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -537,8 +538,8 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
// de-spaghetti the code. // de-spaghetti the code.
defer func() { defer func() {
if err := containers.Remove(ic.ClientCxt, con.ID, bindings.PFalse, bindings.PTrue); err != nil { if err := containers.Remove(ic.ClientCxt, con.ID, bindings.PFalse, bindings.PTrue); err != nil {
if errors.Cause(err) == define.ErrNoSuchCtr || if errorhandling.Contains(err, define.ErrNoSuchCtr) ||
errors.Cause(err) == define.ErrCtrRemoved { errorhandling.Contains(err, define.ErrCtrRemoved) {
logrus.Warnf("Container %s does not exist: %v", con.ID, err) logrus.Warnf("Container %s does not exist: %v", con.ID, err)
} else { } else {
logrus.Errorf("Error removing container %s: %v", con.ID, err) logrus.Errorf("Error removing container %s: %v", con.ID, err)
@ -556,7 +557,7 @@ func (ic *ContainerEngine) ContainerRun(ctx context.Context, opts entities.Conta
// Determine why the wait failed. If the container doesn't exist, // Determine why the wait failed. If the container doesn't exist,
// consult the events. // consult the events.
if !strings.Contains(waitErr.Error(), define.ErrNoSuchCtr.Error()) { if !errorhandling.Contains(waitErr, define.ErrNoSuchCtr) {
return &report, waitErr return &report, waitErr
} }

View File

@ -57,3 +57,11 @@ func CloseQuiet(f *os.File) {
logrus.Errorf("unable to close file %s: %q", f.Name(), err) logrus.Errorf("unable to close file %s: %q", f.Name(), err)
} }
} }
// Contains checks if err's message contains sub's message. Contains should be
// used iff either err or sub has lost type information (e.g., due to
// marshaling). For typed errors, please use `errors.Contains(...)` or `Is()`
// in recent version of Go.
func Contains(err error, sub error) bool {
return strings.Contains(err.Error(), sub.Error())
}