From 4878dff3e2c89382699c29c10dc5036367275575 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 5 Oct 2020 12:33:53 -0700 Subject: Remove excessive error wrapping In case os.Open[File], os.Mkdir[All], ioutil.ReadFile and the like fails, the error message already contains the file name and the operation that fails, so there is no need to wrap the error with something like "open %s failed". While at it - replace a few places with os.Open, ioutil.ReadAll with ioutil.ReadFile. - replace errors.Wrapf with errors.Wrap for cases where there are no %-style arguments. Signed-off-by: Kir Kolyshkin --- pkg/domain/infra/abi/containers.go | 2 +- pkg/domain/infra/abi/cp.go | 6 +++--- pkg/domain/infra/abi/images.go | 2 +- pkg/domain/infra/abi/play.go | 6 +++--- pkg/domain/infra/abi/trust.go | 4 ++-- pkg/domain/infra/tunnel/containers.go | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) (limited to 'pkg/domain') diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go index 8b0d53940..bd6380f75 100644 --- a/pkg/domain/infra/abi/containers.go +++ b/pkg/domain/infra/abi/containers.go @@ -604,7 +604,7 @@ func checkExecPreserveFDs(options entities.ExecOptions) error { if options.PreserveFDs > 0 { entries, err := ioutil.ReadDir("/proc/self/fd") if err != nil { - return errors.Wrapf(err, "unable to read /proc/self/fd") + return err } m := make(map[int]bool) diff --git a/pkg/domain/infra/abi/cp.go b/pkg/domain/infra/abi/cp.go index 0cd2ac8ca..a0bfcc90c 100644 --- a/pkg/domain/infra/abi/cp.go +++ b/pkg/domain/infra/abi/cp.go @@ -115,7 +115,7 @@ func (ic *ContainerEngine) ContainerCp(ctx context.Context, source, dest string, return nil, err } if err = idtools.MkdirAllAndChownNew(ctrWorkDir, 0755, hostOwner); err != nil { - return nil, errors.Wrapf(err, "error creating directory %q", destPath) + return nil, err } cleanedPath, err := securejoin.SecureJoin(mountPoint, filepath.Join(ctr.WorkingDir(), destPath)) if err != nil { @@ -249,7 +249,7 @@ func containerCopy(srcPath, destPath, src, dest string, idMappingOpts storage.ID } destDirIsExist := err == nil if err = os.MkdirAll(destdir, 0755); err != nil { - return errors.Wrapf(err, "error creating directory %q", destdir) + return err } // return functions for copying items @@ -351,7 +351,7 @@ func streamFileToStdout(srcPath string, srcfi os.FileInfo) error { file, err := os.Open(srcPath) if err != nil { - return errors.Wrapf(err, "error opening file %s", srcPath) + return err } defer file.Close() if !archive.IsArchivePath(srcPath) { diff --git a/pkg/domain/infra/abi/images.go b/pkg/domain/infra/abi/images.go index 965c63bec..3bb7de83c 100644 --- a/pkg/domain/infra/abi/images.go +++ b/pkg/domain/infra/abi/images.go @@ -770,7 +770,7 @@ func (ir *ImageEngine) Sign(ctx context.Context, names []string, options entitie if err := os.MkdirAll(signatureDir, 0751); err != nil { // The directory is allowed to exist if !os.IsExist(err) { - logrus.Errorf("error creating directory %s: %s", signatureDir, err) + logrus.Error(err) continue } } diff --git a/pkg/domain/infra/abi/play.go b/pkg/domain/infra/abi/play.go index aa6aeede2..02bf91d53 100644 --- a/pkg/domain/infra/abi/play.go +++ b/pkg/domain/infra/abi/play.go @@ -248,7 +248,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY case v1.HostPathDirectoryOrCreate: if _, err := os.Stat(hostPath.Path); os.IsNotExist(err) { if err := os.Mkdir(hostPath.Path, kubeDirectoryPermission); err != nil { - return nil, errors.Errorf("Error creating HostPath %s at %s", volume.Name, hostPath.Path) + return nil, errors.Errorf("Error creating HostPath %s", volume.Name) } } // Label a newly created volume @@ -259,7 +259,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY if _, err := os.Stat(hostPath.Path); os.IsNotExist(err) { f, err := os.OpenFile(hostPath.Path, os.O_RDONLY|os.O_CREATE, kubeFilePermission) if err != nil { - return nil, errors.Errorf("Error creating HostPath %s at %s", volume.Name, hostPath.Path) + return nil, errors.Errorf("Error creating HostPath %s", volume.Name) } if err := f.Close(); err != nil { logrus.Warnf("Error in closing newly created HostPath file: %v", err) @@ -272,7 +272,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY case v1.HostPathSocket: st, err := os.Stat(hostPath.Path) if err != nil { - return nil, errors.Wrapf(err, "Error checking HostPathSocket") + return nil, errors.Wrap(err, "Error checking HostPathSocket") } if st.Mode()&os.ModeSocket != os.ModeSocket { return nil, errors.Errorf("Error checking HostPathSocket: path %s is not a socket", hostPath.Path) diff --git a/pkg/domain/infra/abi/trust.go b/pkg/domain/infra/abi/trust.go index c697722ee..4a12297f9 100644 --- a/pkg/domain/infra/abi/trust.go +++ b/pkg/domain/infra/abi/trust.go @@ -24,7 +24,7 @@ func (ir *ImageEngine) ShowTrust(ctx context.Context, args []string, options ent } report.Raw, err = ioutil.ReadFile(policyPath) if err != nil { - return nil, errors.Wrapf(err, "unable to read %s", policyPath) + return nil, err } if options.Raw { return &report, nil @@ -67,7 +67,7 @@ func (ir *ImageEngine) SetTrust(ctx context.Context, args []string, options enti if !os.IsNotExist(err) { policyContent, err := ioutil.ReadFile(policyPath) if err != nil { - return errors.Wrapf(err, "unable to read %s", policyPath) + return err } if err := json.Unmarshal(policyContent, &policyContentStruct); err != nil { return errors.Errorf("could not read trust policies") diff --git a/pkg/domain/infra/tunnel/containers.go b/pkg/domain/infra/tunnel/containers.go index 194bb4b48..1bb4e68ac 100644 --- a/pkg/domain/infra/tunnel/containers.go +++ b/pkg/domain/infra/tunnel/containers.go @@ -84,7 +84,7 @@ func (ic *ContainerEngine) ContainerStop(ctx context.Context, namesOrIds []strin for _, cidFile := range options.CIDFiles { content, err := ioutil.ReadFile(cidFile) if err != nil { - return nil, errors.Wrapf(err, "error reading CIDFile %s", cidFile) + return nil, errors.Wrap(err, "error reading CIDFile") } id := strings.Split(string(content), "\n")[0] namesOrIds = append(namesOrIds, id) @@ -164,7 +164,7 @@ func (ic *ContainerEngine) ContainerRm(ctx context.Context, namesOrIds []string, for _, cidFile := range options.CIDFiles { content, err := ioutil.ReadFile(cidFile) if err != nil { - return nil, errors.Wrapf(err, "error reading CIDFile %s", cidFile) + return nil, errors.Wrap(err, "error reading CIDFile") } id := strings.Split(string(content), "\n")[0] namesOrIds = append(namesOrIds, id) -- cgit v1.2.3-54-g00ecf