Skip to content

Commit

Permalink
Fix the "err: cause" order of OCI runtime errors
Browse files Browse the repository at this point in the history
Previously, the order of OCI error messages was reversed, so that the
type of error was listed as the cause. For example:

    Error: writing file `cpu.cfs_quota_us`: Invalid argument: OCI runtime error

This error message makes it seem like "OCI runtime error" is the
argument that was invalid. In fact, "OCI runtime error" is the error and
"writing file ..." is the cause. With this change, the above message
reads:

    Error: OCI runtime error: writing file `cpu.cfs_quota_us`: Invalid argument

Signed-off-by: Jordan Christiansen <[email protected]>
  • Loading branch information
xordspar0 authored and mheon committed Oct 14, 2020
1 parent 9be7650 commit b5f7ed1
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 b5f7ed1

Please sign in to comment.