Skip to content

Commit

Permalink
Merge pull request #7968 from xordspar0/oci-runtime-error
Browse files Browse the repository at this point in the history
Print the correct underlying cause for OCI errors
  • Loading branch information
openshift-merge-robot committed Oct 9, 2020
2 parents ffabd57 + c47a1b1 commit cec2403
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/podman/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v2/cmd/podman/registry"
"github.com/containers/podman/v2/cmd/podman/validate"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/pkg/domain/entities"
"github.com/containers/podman/v2/pkg/parallel"
"github.com/containers/podman/v2/pkg/rootless"
Expand Down Expand Up @@ -84,7 +85,7 @@ func init() {

func Execute() {
if err := rootCmd.ExecuteContext(registry.GetContextWithOptions()); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err.Error())
fmt.Fprintln(os.Stderr, formatError(err))
} else if registry.GetExitCode() == registry.ExecErrorCodeGeneric {
// The exitCode modified from registry.ExecErrorCodeGeneric,
// indicates an application
Expand Down Expand Up @@ -331,3 +332,19 @@ func resolveDestination() (string, string, string) {
}
return cfg.Engine.ActiveService, uri, ident
}

func formatError(err error) string {
var message string
if errors.Cause(err) == define.ErrOCIRuntime {
// OCIRuntimeErrors include the reason for the failure in the
// second to last message in the error chain.
message = fmt.Sprintf(
"Error: %s: %s",
define.ErrOCIRuntime.Error(),
strings.TrimSuffix(err.Error(), ": "+define.ErrOCIRuntime.Error()),
)
} else {
message = "Error: " + err.Error()
}
return message
}
34 changes: 34 additions & 0 deletions cmd/podman/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"fmt"
"strings"
"testing"

"github.com/containers/podman/v2/libpod/define"
"github.com/pkg/errors"
)

func TestFormatError(t *testing.T) {
err := errors.New("unknown error")
output := formatError(err)
expected := fmt.Sprintf("Error: %v", err)

if output != expected {
t.Errorf("Expected \"%s\" to equal \"%s\"", output, err.Error())
}
}

func TestFormatOCIError(t *testing.T) {
expectedPrefix := "Error: "
expectedSuffix := "OCI runtime output"
err := errors.Wrap(define.ErrOCIRuntime, expectedSuffix)
output := formatError(err)

if !strings.HasPrefix(output, expectedPrefix) {
t.Errorf("Expected \"%s\" to start with \"%s\"", output, expectedPrefix)
}
if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("Expected \"%s\" to end with \"%s\"", output, expectedSuffix)
}
}

0 comments on commit cec2403

Please sign in to comment.