Replace fmt.Errorf w/o %-style to errors.New

Using fmt.Errorf for errors that do not have %-style formatting
directives is an overkill. Switch to errors.New.

Found by

	git grep fmt.Errorf | grep -v ^vendor | grep -v '%'

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-06-08 11:24:00 -07:00
parent 242b3283fd
commit 627a06ad92
13 changed files with 57 additions and 42 deletions
+3 -2
View File
@@ -17,6 +17,7 @@
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
@@ -88,7 +89,7 @@ func handleSingle(path string, noStdin bool) error {
// Get the fd of the connection.
unixconn, ok := conn.(*net.UnixConn)
if !ok {
return fmt.Errorf("failed to cast to unixconn")
return errors.New("failed to cast to unixconn")
}
socket, err := unixconn.File()
@@ -217,7 +218,7 @@ func main() {
app.Action = func(ctx *cli.Context) error {
args := ctx.Args()
if len(args) != 1 {
return fmt.Errorf("need to specify a single socket path")
return errors.New("need to specify a single socket path")
}
path := ctx.Args()[0]
+3 -2
View File
@@ -4,6 +4,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
@@ -115,11 +116,11 @@ func execProcess(context *cli.Context) (int, error) {
return -1, err
}
if status == libcontainer.Stopped {
return -1, fmt.Errorf("cannot exec a container that has stopped")
return -1, errors.New("cannot exec a container that has stopped")
}
path := context.String("process")
if path == "" && len(context.Args()) == 1 {
return -1, fmt.Errorf("process args cannot be empty")
return -1, errors.New("process args cannot be empty")
}
detach := context.Bool("detach")
state, err := container.State()
+2 -2
View File
@@ -4,7 +4,7 @@ package cgroups
import (
"bytes"
"fmt"
"errors"
"reflect"
"strings"
"testing"
@@ -372,7 +372,7 @@ func TestParseCgroupString(t *testing.T) {
},
{
input: `malformed input`,
expectedError: fmt.Errorf(`invalid cgroup entry: must contain at least two colons: malformed input`),
expectedError: errors.New(`invalid cgroup entry: must contain at least two colons: malformed input`),
},
}
+1 -1
View File
@@ -154,7 +154,7 @@ func findCgroupMountpointAndRootFromMI(mounts []*mountinfo.Info, cgroupPath, sub
func (m Mount) GetOwnCgroup(cgroups map[string]string) (string, error) {
if len(m.Subsystems) == 0 {
return "", fmt.Errorf("no subsystem for mount")
return "", errors.New("no subsystem for mount")
}
return getControllerPath(m.Subsystems[0], cgroups)
+12 -5
View File
@@ -1,17 +1,24 @@
package configs
import "fmt"
import "errors"
var (
errNoUIDMap = errors.New("User namespaces enabled, but no uid mappings found.")
errNoUserMap = errors.New("User namespaces enabled, but no user mapping found.")
errNoGIDMap = errors.New("User namespaces enabled, but no gid mappings found.")
errNoGroupMap = errors.New("User namespaces enabled, but no group mapping found.")
)
// HostUID gets the translated uid for the process on host which could be
// different when user namespaces are enabled.
func (c Config) HostUID(containerId int) (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.UidMappings == nil {
return -1, fmt.Errorf("User namespaces enabled, but no uid mappings found.")
return -1, errNoUIDMap
}
id, found := c.hostIDFromMapping(containerId, c.UidMappings)
if !found {
return -1, fmt.Errorf("User namespaces enabled, but no user mapping found.")
return -1, errNoUserMap
}
return id, nil
}
@@ -30,11 +37,11 @@ func (c Config) HostRootUID() (int, error) {
func (c Config) HostGID(containerId int) (int, error) {
if c.Namespaces.Contains(NEWUSER) {
if c.GidMappings == nil {
return -1, fmt.Errorf("User namespaces enabled, but no gid mappings found.")
return -1, errNoGIDMap
}
id, found := c.hostIDFromMapping(containerId, c.GidMappings)
if !found {
return -1, fmt.Errorf("User namespaces enabled, but no group mapping found.")
return -1, errNoGroupMap
}
return id, nil
}
+1 -1
View File
@@ -666,7 +666,7 @@ func (c *linuxContainer) Resume() error {
return err
}
if status != Paused {
return newGenericError(fmt.Errorf("container not paused"), ContainerNotPaused)
return newGenericError(errors.New("container not paused"), ContainerNotPaused)
}
if err := c.cgroupManager.Freeze(configs.Thawed); err != nil {
return err
+9 -6
View File
@@ -31,7 +31,10 @@ const (
execFifoFilename = "exec.fifo"
)
var idRegex = regexp.MustCompile(`^[\w+-\.]+$`)
var (
idRegex = regexp.MustCompile(`^[\w+-\.]+$`)
errNoSystemd = errors.New("systemd not running on this host, can't use systemd as cgroups manager")
)
// InitArgs returns an options func to configure a LinuxFactory with the
// provided init binary path and arguments.
@@ -80,7 +83,7 @@ func systemdCgroupV2(l *LinuxFactory, rootless bool) error {
// containers that use systemd to create and manage cgroups.
func SystemdCgroups(l *LinuxFactory) error {
if !systemd.IsRunningSystemd() {
return fmt.Errorf("systemd not running on this host, can't use systemd as cgroups manager")
return errNoSystemd
}
if cgroups.IsCgroup2UnifiedMode() {
@@ -97,11 +100,11 @@ func SystemdCgroups(l *LinuxFactory) error {
// RootlessSystemdCgroups is rootless version of SystemdCgroups.
func RootlessSystemdCgroups(l *LinuxFactory) error {
if !systemd.IsRunningSystemd() {
return fmt.Errorf("systemd not running on this host, can't use systemd as cgroups manager")
return errNoSystemd
}
if !cgroups.IsCgroup2UnifiedMode() {
return fmt.Errorf("cgroup v2 not enabled on this host, can't use systemd (rootless) as cgroups manager")
return errors.New("cgroup v2 not enabled on this host, can't use systemd (rootless) as cgroups manager")
}
return systemdCgroupV2(l, true)
}
@@ -246,7 +249,7 @@ type LinuxFactory struct {
func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, error) {
if l.Root == "" {
return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid)
return nil, newGenericError(errors.New("invalid root"), ConfigInvalid)
}
if err := l.validateID(id); err != nil {
return nil, err
@@ -289,7 +292,7 @@ func (l *LinuxFactory) Create(id string, config *configs.Config) (Container, err
func (l *LinuxFactory) Load(id string) (Container, error) {
if l.Root == "" {
return nil, newGenericError(fmt.Errorf("invalid root"), ConfigInvalid)
return nil, newGenericError(errors.New("invalid root"), ConfigInvalid)
}
// when load, we need to check id is valid or not.
if err := l.validateID(id); err != nil {
+4 -4
View File
@@ -1,20 +1,20 @@
package libcontainer
import (
"fmt"
"errors"
"io/ioutil"
"testing"
)
func TestErrorDetail(t *testing.T) {
err := newGenericError(fmt.Errorf("test error"), SystemError)
err := newGenericError(errors.New("test error"), SystemError)
if derr := err.Detail(ioutil.Discard); derr != nil {
t.Fatal(derr)
}
}
func TestErrorWithCode(t *testing.T) {
err := newGenericError(fmt.Errorf("test error"), SystemError)
err := newGenericError(errors.New("test error"), SystemError)
if code := err.Code(); code != SystemError {
t.Fatalf("expected err code %q but %q", SystemError, code)
}
@@ -35,7 +35,7 @@ func TestErrorWithError(t *testing.T) {
}
for _, v := range cc {
err := newSystemErrorWithCause(fmt.Errorf(v.errmsg), v.cause)
err := newSystemErrorWithCause(errors.New(v.errmsg), v.cause)
msg := err.Error()
if v.cause == "" && msg != v.errmsg {
+6 -4
View File
@@ -1,7 +1,7 @@
package libcontainer
import (
"fmt"
"errors"
"io"
"math"
"os"
@@ -9,6 +9,8 @@ import (
"github.com/opencontainers/runc/libcontainer/configs"
)
var errInvalidProcess = errors.New("invalid process")
type processOperations interface {
wait() (*os.ProcessState, error)
signal(sig os.Signal) error
@@ -84,7 +86,7 @@ type Process struct {
// Wait releases any resources associated with the Process
func (p Process) Wait() (*os.ProcessState, error) {
if p.ops == nil {
return nil, newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
return nil, newGenericError(errInvalidProcess, NoProcessOps)
}
return p.ops.wait()
}
@@ -94,7 +96,7 @@ func (p Process) Pid() (int, error) {
// math.MinInt32 is returned here, because it's invalid value
// for the kill() system call.
if p.ops == nil {
return math.MinInt32, newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
return math.MinInt32, newGenericError(errInvalidProcess, NoProcessOps)
}
return p.ops.pid(), nil
}
@@ -102,7 +104,7 @@ func (p Process) Pid() (int, error) {
// Signal sends a signal to the Process.
func (p Process) Signal(sig os.Signal) error {
if p.ops == nil {
return newGenericError(fmt.Errorf("invalid process"), NoProcessOps)
return newGenericError(errInvalidProcess, NoProcessOps)
}
return p.ops.signal(sig)
}
+5 -5
View File
@@ -3,7 +3,7 @@
package libcontainer
import (
"fmt"
"errors"
"os"
"os/exec"
@@ -31,7 +31,7 @@ type restoredProcess struct {
}
func (p *restoredProcess) start() error {
return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError)
return newGenericError(errors.New("restored process cannot be started"), SystemError)
}
func (p *restoredProcess) pid() int {
@@ -89,7 +89,7 @@ type nonChildProcess struct {
}
func (p *nonChildProcess) start() error {
return newGenericError(fmt.Errorf("restored process cannot be started"), SystemError)
return newGenericError(errors.New("restored process cannot be started"), SystemError)
}
func (p *nonChildProcess) pid() int {
@@ -97,11 +97,11 @@ func (p *nonChildProcess) pid() int {
}
func (p *nonChildProcess) terminate() error {
return newGenericError(fmt.Errorf("restored process cannot be terminated"), SystemError)
return newGenericError(errors.New("restored process cannot be terminated"), SystemError)
}
func (p *nonChildProcess) wait() (*os.ProcessState, error) {
return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError)
return nil, newGenericError(errors.New("restored process cannot be waited on"), SystemError)
}
func (p *nonChildProcess) startTime() (uint64, error) {
+3 -3
View File
@@ -216,7 +216,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
}
spec := opts.Spec
if spec.Root == nil {
return nil, fmt.Errorf("Root must be specified")
return nil, errors.New("Root must be specified")
}
rootfsPath := spec.Root.Path
if !filepath.IsAbs(rootfsPath) {
@@ -263,7 +263,7 @@ func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) {
return nil, fmt.Errorf("rootfsPropagation=%v is not supported", spec.Linux.RootfsPropagation)
}
if config.NoPivotRoot && (config.RootPropagation&unix.MS_PRIVATE != 0) {
return nil, fmt.Errorf("rootfsPropagation of [r]private is not safe without pivot_root")
return nil, errors.New("rootfsPropagation of [r]private is not safe without pivot_root")
}
for _, ns := range spec.Linux.Namespaces {
@@ -858,7 +858,7 @@ func SetupSeccomp(config *specs.LinuxSeccomp) (*configs.Seccomp, error) {
// We don't currently support seccomp flags.
if len(config.Flags) != 0 {
return nil, fmt.Errorf("seccomp flags are not yet supported by runc")
return nil, errors.New("seccomp flags are not yet supported by runc")
}
newConfig := new(configs.Seccomp)
+4 -3
View File
@@ -3,6 +3,7 @@
package libcontainer
import (
"errors"
"fmt"
"os"
"path/filepath"
@@ -117,7 +118,7 @@ func (r *runningState) transition(s containerState) error {
switch s.(type) {
case *stoppedState:
if r.c.runType() == Running {
return newGenericError(fmt.Errorf("container still running"), ContainerNotStopped)
return newGenericError(errors.New("container still running"), ContainerNotStopped)
}
r.c.state = s
return nil
@@ -132,7 +133,7 @@ func (r *runningState) transition(s containerState) error {
func (r *runningState) destroy() error {
if r.c.runType() == Running {
return newGenericError(fmt.Errorf("container is not destroyed"), ContainerNotStopped)
return newGenericError(errors.New("container is not destroyed"), ContainerNotStopped)
}
return destroy(r.c)
}
@@ -190,7 +191,7 @@ func (p *pausedState) destroy() error {
}
return destroy(p.c)
}
return newGenericError(fmt.Errorf("container is paused"), ContainerPaused)
return newGenericError(errors.New("container is paused"), ContainerPaused)
}
// restoredState is the same as the running state but also has associated checkpoint
+4 -4
View File
@@ -119,7 +119,7 @@ func ParsePasswdFileFilter(path string, filter func(User) bool) ([]User, error)
func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error) {
if r == nil {
return nil, fmt.Errorf("nil source for passwd-formatted data")
return nil, errors.New("nil source for passwd-formatted data")
}
var (
@@ -177,7 +177,7 @@ func ParseGroupFileFilter(path string, filter func(Group) bool) ([]Group, error)
func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error) {
if r == nil {
return nil, fmt.Errorf("nil source for group-formatted data")
return nil, errors.New("nil source for group-formatted data")
}
var (
@@ -487,7 +487,7 @@ func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error)
func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error) {
if r == nil {
return nil, fmt.Errorf("nil source for subid-formatted data")
return nil, errors.New("nil source for subid-formatted data")
}
var (
@@ -540,7 +540,7 @@ func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error)
func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error) {
if r == nil {
return nil, fmt.Errorf("nil source for idmap-formatted data")
return nil, errors.New("nil source for idmap-formatted data")
}
var (