Fix errorf conventions

Signed-off-by: Brandon Lum <lumjjb@gmail.com>
This commit is contained in:
Brandon Lum 2020-04-03 20:34:43 +00:00
parent e9a6703ede
commit 40df1c6e3b
12 changed files with 40 additions and 42 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"fmt"
"os" "os"
"time" "time"
@ -60,7 +59,7 @@ func getStore(c *cobra.Command) (storage.Store, error) {
// Differently, allow the mount if we are already in a userns, as the mount point will still // Differently, allow the mount if we are already in a userns, as the mount point will still
// be accessible once "buildah mount" exits. // be accessible once "buildah mount" exits.
if os.Geteuid() != 0 && options.GraphDriverName != "vfs" { if os.Geteuid() != 0 && options.GraphDriverName != "vfs" {
return nil, fmt.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", options.GraphDriverName) return nil, errors.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", options.GraphDriverName)
} }
// For uid/gid mappings, first we check the global definitions // For uid/gid mappings, first we check the global definitions

View File

@ -188,21 +188,21 @@ func parseFilter(ctx context.Context, store storage.Store, images []storage.Imag
if pair[1] == "true" || pair[1] == "false" { if pair[1] == "true" || pair[1] == "false" {
params.dangling = pair[1] params.dangling = pair[1]
} else { } else {
return nil, fmt.Errorf("invalid filter: '%s=[%s]'", pair[0], pair[1]) return nil, errors.Errorf("invalid filter: '%s=[%s]'", pair[0], pair[1])
} }
case "label": case "label":
params.label = pair[1] params.label = pair[1]
case "before": case "before":
beforeDate, err := setFilterDate(ctx, store, images, pair[1]) beforeDate, err := setFilterDate(ctx, store, images, pair[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("no such id: %s", pair[0]) return nil, errors.Errorf("no such id: %s", pair[0])
} }
params.beforeDate = beforeDate params.beforeDate = beforeDate
params.beforeImage = pair[1] params.beforeImage = pair[1]
case "since": case "since":
sinceDate, err := setFilterDate(ctx, store, images, pair[1]) sinceDate, err := setFilterDate(ctx, store, images, pair[1])
if err != nil { if err != nil {
return nil, fmt.Errorf("no such id: %s", pair[0]) return nil, errors.Errorf("no such id: %s", pair[0])
} }
params.sinceDate = sinceDate params.sinceDate = sinceDate
params.sinceImage = pair[1] params.sinceImage = pair[1]
@ -212,10 +212,10 @@ func parseFilter(ctx context.Context, store storage.Store, images []storage.Imag
if pair[1] == "true" || pair[1] == "false" { if pair[1] == "true" || pair[1] == "false" {
params.readOnly = pair[1] params.readOnly = pair[1]
} else { } else {
return nil, fmt.Errorf("invalid filter: '%s=[%s]'", pair[0], pair[1]) return nil, errors.Errorf("invalid filter: '%s=[%s]'", pair[0], pair[1])
} }
default: default:
return nil, fmt.Errorf("invalid filter: '%s'", pair[0]) return nil, errors.Errorf("invalid filter: '%s'", pair[0])
} }
} }
return params, nil return params, nil
@ -228,23 +228,23 @@ func setFilterDate(ctx context.Context, store storage.Store, images []storage.Im
// Set the date to this image // Set the date to this image
ref, err := is.Transport.ParseStoreReference(store, image.ID) ref, err := is.Transport.ParseStoreReference(store, image.ID)
if err != nil { if err != nil {
return time.Time{}, fmt.Errorf("error parsing reference to image %q: %v", image.ID, err) return time.Time{}, errors.Wrapf(err, "error parsing reference to image %q", image.ID)
} }
img, err := ref.NewImage(ctx, nil) img, err := ref.NewImage(ctx, nil)
if err != nil { if err != nil {
return time.Time{}, fmt.Errorf("error reading image %q: %v", image.ID, err) return time.Time{}, errors.Wrapf(err, "error reading image %q", image.ID)
} }
defer img.Close() defer img.Close()
inspect, err := img.Inspect(ctx) inspect, err := img.Inspect(ctx)
if err != nil { if err != nil {
return time.Time{}, fmt.Errorf("error inspecting image %q: %v", image.ID, err) return time.Time{}, errors.Wrapf(err, "error inspecting image %q", image.ID)
} }
date := *inspect.Created date := *inspect.Created
return date, nil return date, nil
} }
} }
} }
return time.Time{}, fmt.Errorf("could not locate image %q", imgName) return time.Time{}, errors.Errorf("could not locate image %q", imgName)
} }
func outputHeader(opts imageOptions) string { func outputHeader(opts imageOptions) string {

View File

@ -679,7 +679,7 @@ func manifestPushCmd(c *cobra.Command, args []string, opts manifestPushOpts) err
case "v2s2", "docker": case "v2s2", "docker":
manifestType = manifest.DockerV2Schema2MediaType manifestType = manifest.DockerV2Schema2MediaType
default: default:
return fmt.Errorf("unknown format %q. Choose on of the supported formats: 'oci' or 'v2s2'", opts.format) return errors.Errorf("unknown format %q. Choose on of the supported formats: 'oci' or 'v2s2'", opts.format)
} }
} }

View File

@ -63,7 +63,7 @@ func mountCmd(c *cobra.Command, args []string, noTruncate bool) error {
// Differently, allow the mount if we are already in a userns, as the mount point will still // Differently, allow the mount if we are already in a userns, as the mount point will still
// be accessible once "buildah mount" exits. // be accessible once "buildah mount" exits.
if os.Geteuid() != 0 && store.GraphDriverName() != "vfs" { if os.Geteuid() != 0 && store.GraphDriverName() != "vfs" {
return fmt.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", store.GraphDriverName()) return errors.Errorf("cannot mount using driver %s in rootless mode. You need to run it in a `buildah unshare` session", store.GraphDriverName())
} }
for _, name := range args { for _, name := range args {

View File

@ -160,7 +160,7 @@ func pushCmd(c *cobra.Command, args []string, iopts pushOptions) error {
case "v2s2", "docker": case "v2s2", "docker":
manifestType = manifest.DockerV2Schema2MediaType manifestType = manifest.DockerV2Schema2MediaType
default: default:
return fmt.Errorf("unknown format %q. Choose on of the supported formats: 'oci', 'v2s1', or 'v2s2'", iopts.format) return errors.Errorf("unknown format %q. Choose on of the supported formats: 'oci', 'v2s1', or 'v2s2'", iopts.format)
} }
} }

View File

@ -3,7 +3,6 @@ package imagebuildah
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -323,7 +322,7 @@ func preprocessDockerfileContents(r io.Reader, ctxDir string) (rdrCloser *io.Rea
pipe.Close() pipe.Close()
if err = cmd.Wait(); err != nil { if err = cmd.Wait(); err != nil {
if stderr.Len() > 0 { if stderr.Len() > 0 {
err = fmt.Errorf("%v: %s", err, strings.TrimSpace(stderr.String())) err = errors.Wrapf(err, "%v", strings.TrimSpace(stderr.String()))
} }
return nil, errors.Wrapf(err, "error pre-processing Dockerfile") return nil, errors.Wrapf(err, "error pre-processing Dockerfile")
} }

View File

@ -15,6 +15,7 @@ import (
"github.com/containers/storage" "github.com/containers/storage"
"github.com/containers/storage/pkg/system" "github.com/containers/storage/pkg/system"
"github.com/containers/storage/pkg/unshare" "github.com/containers/storage/pkg/unshare"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -184,7 +185,7 @@ func readUptime() (string, error) {
} }
f := bytes.Fields(buf) f := bytes.Fields(buf)
if len(f) < 1 { if len(f) < 1 {
return "", fmt.Errorf("invalid uptime") return "", errors.Errorf("invalid uptime")
} }
return string(f[0]), nil return string(f[0]), nil
} }

6
new.go
View File

@ -196,7 +196,7 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
logrus.Debugf("no such image %q: %v", transports.ImageName(ref), err) logrus.Debugf("no such image %q: %v", transports.ImageName(ref), err)
failures = append(failures, failure{ failures = append(failures, failure{
resolvedImageName: image, resolvedImageName: image,
err: fmt.Errorf("no such image %q", transports.ImageName(ref)), err: errors.Errorf("no such image %q", transports.ImageName(ref)),
}) })
continue continue
} }
@ -212,7 +212,7 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
} }
if len(failures) != len(candidates) { if len(failures) != len(candidates) {
return nil, "", nil, fmt.Errorf("internal error: %d candidates (%#v) vs. %d failures (%#v)", len(candidates), candidates, len(failures), failures) return nil, "", nil, errors.Errorf("internal error: %d candidates (%#v) vs. %d failures (%#v)", len(candidates), candidates, len(failures), failures)
} }
registriesConfPath := sysregistriesv2.ConfigPath(systemContext) registriesConfPath := sysregistriesv2.ConfigPath(systemContext)
@ -221,7 +221,7 @@ func resolveImage(ctx context.Context, systemContext *types.SystemContext, store
if searchRegistriesWereUsedButEmpty { if searchRegistriesWereUsedButEmpty {
return nil, "", nil, errors.Errorf("image name %q is a short name and no search registries are defined in %s.", options.FromImage, registriesConfPath) return nil, "", nil, errors.Errorf("image name %q is a short name and no search registries are defined in %s.", options.FromImage, registriesConfPath)
} }
return nil, "", nil, fmt.Errorf("internal error: no pull candidates were available for %q for an unknown reason", options.FromImage) return nil, "", nil, errors.Errorf("internal error: no pull candidates were available for %q for an unknown reason", options.FromImage)
case 1: case 1:
err := failures[0].err err := failures[0].err

View File

@ -537,10 +537,10 @@ func validateExtraHost(val string) error {
// allow for IPv6 addresses in extra hosts by only splitting on first ":" // allow for IPv6 addresses in extra hosts by only splitting on first ":"
arr := strings.SplitN(val, ":", 2) arr := strings.SplitN(val, ":", 2)
if len(arr) != 2 || len(arr[0]) == 0 { if len(arr) != 2 || len(arr[0]) == 0 {
return fmt.Errorf("bad format for add-host: %q", val) return errors.Errorf("bad format for add-host: %q", val)
} }
if _, err := validateIPAddress(arr[1]); err != nil { if _, err := validateIPAddress(arr[1]); err != nil {
return fmt.Errorf("invalid IP address in add-host: %q", arr[1]) return errors.Errorf("invalid IP address in add-host: %q", arr[1])
} }
return nil return nil
} }
@ -552,7 +552,7 @@ func validateIPAddress(val string) (string, error) {
if ip != nil { if ip != nil {
return ip.String(), nil return ip.String(), nil
} }
return "", fmt.Errorf("%s is not an ip address", val) return "", errors.Errorf("%s is not an ip address", val)
} }
// SystemContextFromOptions returns a SystemContext populated with values // SystemContextFromOptions returns a SystemContext populated with values
@ -814,20 +814,20 @@ func parseIDMap(spec []string) (m [][3]uint32, err error) {
for _, s := range spec { for _, s := range spec {
args := strings.FieldsFunc(s, func(r rune) bool { return !unicode.IsDigit(r) }) args := strings.FieldsFunc(s, func(r rune) bool { return !unicode.IsDigit(r) })
if len(args)%3 != 0 { if len(args)%3 != 0 {
return nil, fmt.Errorf("mapping %q is not in the form containerid:hostid:size[,...]", s) return nil, errors.Errorf("mapping %q is not in the form containerid:hostid:size[,...]", s)
} }
for len(args) >= 3 { for len(args) >= 3 {
cid, err := strconv.ParseUint(args[0], 10, 32) cid, err := strconv.ParseUint(args[0], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing container ID %q from mapping %q as a number: %v", args[0], s, err) return nil, errors.Wrapf(err, "error parsing container ID %q from mapping %q as a number", args[0], s)
} }
hostid, err := strconv.ParseUint(args[1], 10, 32) hostid, err := strconv.ParseUint(args[1], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing host ID %q from mapping %q as a number: %v", args[1], s, err) return nil, errors.Wrapf(err, "error parsing host ID %q from mapping %q as a number", args[1], s)
} }
size, err := strconv.ParseUint(args[2], 10, 32) size, err := strconv.ParseUint(args[2], 10, 32)
if err != nil { if err != nil {
return nil, fmt.Errorf("error parsing %q from mapping %q as a number: %v", args[2], s, err) return nil, errors.Wrapf(err, "error parsing %q from mapping %q as a number", args[2], s)
} }
m = append(m, [3]uint32{uint32(cid), uint32(hostid), uint32(size)}) m = append(m, [3]uint32{uint32(cid), uint32(hostid), uint32(size)})
args = args[3:] args = args[3:]
@ -960,7 +960,7 @@ func Device(device string) (string, string, string, error) {
switch len(arr) { switch len(arr) {
case 3: case 3:
if !isValidDeviceMode(arr[2]) { if !isValidDeviceMode(arr[2]) {
return "", "", "", fmt.Errorf("invalid device mode: %s", arr[2]) return "", "", "", errors.Errorf("invalid device mode: %s", arr[2])
} }
permissions = arr[2] permissions = arr[2]
fallthrough fallthrough
@ -969,7 +969,7 @@ func Device(device string) (string, string, string, error) {
permissions = arr[1] permissions = arr[1]
} else { } else {
if len(arr[1]) == 0 || arr[1][0] != '/' { if len(arr[1]) == 0 || arr[1][0] != '/' {
return "", "", "", fmt.Errorf("invalid device mode: %s", arr[1]) return "", "", "", errors.Errorf("invalid device mode: %s", arr[1])
} }
dst = arr[1] dst = arr[1]
} }
@ -981,7 +981,7 @@ func Device(device string) (string, string, string, error) {
} }
fallthrough fallthrough
default: default:
return "", "", "", fmt.Errorf("invalid device specification: %s", device) return "", "", "", errors.Errorf("invalid device specification: %s", device)
} }
if dst == "" { if dst == "" {

View File

@ -3,9 +3,8 @@
package parse package parse
import ( import (
"fmt"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/pkg/errors"
) )
func getDefaultProcessLimits() []string { func getDefaultProcessLimits() []string {
@ -13,5 +12,5 @@ func getDefaultProcessLimits() []string {
} }
func DeviceFromPath(device string) ([]configs.Device, error) { func DeviceFromPath(device string) ([]configs.Device, error) {
return []configs.Device{}, fmt.Errorf("devices not supported") return []configs.Device{}, errors.Errorf("devices not supported")
} }

View File

@ -2142,7 +2142,7 @@ func checkAndOverrideIsolationOptions(isolation Isolation, options *RunOptions)
pidns := options.NamespaceOptions.Find(string(specs.PIDNamespace)) pidns := options.NamespaceOptions.Find(string(specs.PIDNamespace))
userns := options.NamespaceOptions.Find(string(specs.UserNamespace)) userns := options.NamespaceOptions.Find(string(specs.UserNamespace))
if (pidns == nil || pidns.Host) && (userns != nil && !userns.Host) { if (pidns == nil || pidns.Host) && (userns != nil && !userns.Host) {
return fmt.Errorf("not allowed to mix host PID namespace with container user namespace") return errors.Errorf("not allowed to mix host PID namespace with container user namespace")
} }
} }
return nil return nil

View File

@ -1,11 +1,11 @@
package buildah package buildah
import ( import (
"fmt"
"strings" "strings"
"testing" "testing"
"github.com/opencontainers/runtime-tools/generate" "github.com/opencontainers/runtime-tools/generate"
"github.com/pkg/errors"
) )
func TestAddRlimits(t *testing.T) { func TestAddRlimits(t *testing.T) {
@ -26,11 +26,11 @@ func TestAddRlimits(t *testing.T) {
ulimit: []string{"bla"}, ulimit: []string{"bla"},
test: func(e error, g *generate.Generator) error { test: func(e error, g *generate.Generator) error {
if e == nil { if e == nil {
return fmt.Errorf("expected to receive an error but got nil") return errors.Errorf("expected to receive an error but got nil")
} }
errMsg := "invalid ulimit argument" errMsg := "invalid ulimit argument"
if !strings.Contains(e.Error(), errMsg) { if !strings.Contains(e.Error(), errMsg) {
return fmt.Errorf("expected error message to include %#v in %#v", errMsg, e.Error()) return errors.Errorf("expected error message to include %#v in %#v", errMsg, e.Error())
} }
return nil return nil
}, },
@ -40,11 +40,11 @@ func TestAddRlimits(t *testing.T) {
ulimit: []string{"bla=hard"}, ulimit: []string{"bla=hard"},
test: func(e error, g *generate.Generator) error { test: func(e error, g *generate.Generator) error {
if e == nil { if e == nil {
return fmt.Errorf("expected to receive an error but got nil") return errors.Errorf("expected to receive an error but got nil")
} }
errMsg := "invalid ulimit type" errMsg := "invalid ulimit type"
if !strings.Contains(e.Error(), errMsg) { if !strings.Contains(e.Error(), errMsg) {
return fmt.Errorf("expected error message to include %#v in %#v", errMsg, e.Error()) return errors.Errorf("expected error message to include %#v in %#v", errMsg, e.Error())
} }
return nil return nil
}, },
@ -60,15 +60,15 @@ func TestAddRlimits(t *testing.T) {
for _, rlimit := range rlimits { for _, rlimit := range rlimits {
if rlimit.Type == "RLIMIT_FSIZE" { if rlimit.Type == "RLIMIT_FSIZE" {
if rlimit.Hard != 4096 { if rlimit.Hard != 4096 {
return fmt.Errorf("expected spec to have %#v hard limit set to %v but got %v", rlimit.Type, 4096, rlimit.Hard) return errors.Errorf("expected spec to have %#v hard limit set to %v but got %v", rlimit.Type, 4096, rlimit.Hard)
} }
if rlimit.Soft != 1024 { if rlimit.Soft != 1024 {
return fmt.Errorf("expected spec to have %#v hard limit set to %v but got %v", rlimit.Type, 1024, rlimit.Soft) return errors.Errorf("expected spec to have %#v hard limit set to %v but got %v", rlimit.Type, 1024, rlimit.Soft)
} }
return nil return nil
} }
} }
return fmt.Errorf("expected spec to have RLIMIT_FSIZE") return errors.Errorf("expected spec to have RLIMIT_FSIZE")
}, },
}, },
} }