more error wrapping uses

- replace os.IsNotExist with errors.Is and fs.ErrNotExist
- use %w directive

Signed-off-by: Justen Stall <39888103+justenstall@users.noreply.github.com>
pull/13460/head
Justen Stall 10 months ago
parent b0944e8e7e
commit 6aa19b8c92
No known key found for this signature in database

@ -16,7 +16,9 @@ limitations under the License.
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -202,7 +204,7 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) {
// Make sure tmpcharts-x is deleted // Make sure tmpcharts-x is deleted
tmpPath := filepath.Join(dir(chartname), fmt.Sprintf("tmpcharts-%d", os.Getpid())) tmpPath := filepath.Join(dir(chartname), fmt.Sprintf("tmpcharts-%d", os.Getpid()))
if _, err := os.Stat(tmpPath); !os.IsNotExist(err) { if _, err := os.Stat(tmpPath); !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("tmpcharts dir still exists") t.Fatalf("tmpcharts dir still exists")
} }
} }

@ -265,7 +265,6 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
// As of Helm 2.4.0, this is treated as a stopping condition: // As of Helm 2.4.0, this is treated as a stopping condition:
// https://github.com/helm/helm/issues/2209 // https://github.com/helm/helm/issues/2209
if err := action.CheckDependencies(chartRequested, req); err != nil { if err := action.CheckDependencies(chartRequested, req); err != nil {
err = fmt.Errorf("An error occurred while checking for chart dependencies. You may need to run `helm dependency build` to fetch missing dependencies: %w", err)
if client.DependencyUpdate { if client.DependencyUpdate {
man := &downloader.Manager{ man := &downloader.Manager{
Out: out, Out: out,
@ -286,7 +285,7 @@ func runInstall(args []string, client *action.Install, valueOpts *values.Options
return nil, fmt.Errorf("failed reloading chart after repo update: %w", err) return nil, fmt.Errorf("failed reloading chart after repo update: %w", err)
} }
} else { } else {
return nil, err return nil, fmt.Errorf("An error occurred while checking for chart dependencies. You may need to run `helm dependency build` to fetch missing dependencies: %w", err)
} }
} }
} }

@ -18,8 +18,10 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -138,7 +140,7 @@ func (o *repoAddOptions) run(out io.Writer) error {
} }
b, err := os.ReadFile(o.repoFile) b, err := os.ReadFile(o.repoFile)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err return err
} }

@ -17,8 +17,10 @@ limitations under the License.
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -115,11 +117,11 @@ func TestRepoAdd(t *testing.T) {
} }
idx := filepath.Join(helmpath.CachePath("repository"), helmpath.CacheIndexFile(testRepoName)) idx := filepath.Join(helmpath.CachePath("repository"), helmpath.CacheIndexFile(testRepoName))
if _, err := os.Stat(idx); os.IsNotExist(err) { if _, err := os.Stat(idx); errors.Is(err, fs.ErrNotExist) {
t.Errorf("Error cache index file was not created for repository %s", testRepoName) t.Errorf("Error cache index file was not created for repository %s", testRepoName)
} }
idx = filepath.Join(helmpath.CachePath("repository"), helmpath.CacheChartsFile(testRepoName)) idx = filepath.Join(helmpath.CachePath("repository"), helmpath.CacheChartsFile(testRepoName))
if _, err := os.Stat(idx); os.IsNotExist(err) { if _, err := os.Stat(idx); errors.Is(err, fs.ErrNotExist) {
t.Errorf("Error cache charts file was not created for repository %s", testRepoName) t.Errorf("Error cache charts file was not created for repository %s", testRepoName)
} }

@ -17,8 +17,10 @@ limitations under the License.
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -97,7 +99,7 @@ func index(dir, url, mergeTo string, json bool) error {
if mergeTo != "" { if mergeTo != "" {
// if index.yaml is missing then create an empty one to merge into // if index.yaml is missing then create an empty one to merge into
var i2 *repo.IndexFile var i2 *repo.IndexFile
if _, err := os.Stat(mergeTo); os.IsNotExist(err) { if _, err := os.Stat(mergeTo); errors.Is(err, fs.ErrNotExist) {
i2 = repo.NewIndexFile() i2 = repo.NewIndexFile()
writeIndexFile(i2, mergeTo, json) writeIndexFile(i2, mergeTo, json)
} else { } else {

@ -20,6 +20,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -87,7 +88,7 @@ func removeRepoCache(root, name string) error {
} }
idx = filepath.Join(root, helmpath.CacheIndexFile(name)) idx = filepath.Join(root, helmpath.CacheIndexFile(name))
if _, err := os.Stat(idx); os.IsNotExist(err) { if _, err := os.Stat(idx); errors.Is(err, fs.ErrNotExist) {
return nil return nil
} else if err != nil { } else if err != nil {
return fmt.Errorf("can't remove index file %s: %w", idx, err) return fmt.Errorf("can't remove index file %s: %w", idx, err)

@ -18,8 +18,10 @@ package main
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -254,7 +256,7 @@ func createOrOpenFile(filename string, append bool) (*os.File, error) {
func ensureDirectoryForFile(file string) error { func ensureDirectoryForFile(file string) error {
baseDir := path.Dir(file) baseDir := path.Dir(file)
_, err := os.Stat(baseDir) _, err := os.Stat(baseDir)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err return err
} }

@ -18,7 +18,9 @@ package resolver
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -251,7 +253,7 @@ func GetLocalPath(repo, chartpath string) (string, error) {
depPath = filepath.Join(chartpath, p) depPath = filepath.Join(chartpath, p)
} }
if _, err = os.Stat(depPath); os.IsNotExist(err) { if _, err = os.Stat(depPath); errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("directory %s not found", depPath) return "", fmt.Errorf("directory %s not found", depPath)
} else if err != nil { } else if err != nil {
return "", err return "", err

@ -35,6 +35,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -111,7 +112,7 @@ func CopyDir(src, dst string) error {
} }
_, err = os.Stat(dst) _, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err return err
} }
if err == nil { if err == nil {

@ -19,8 +19,9 @@ package tlsutil
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"errors"
"fmt" "fmt"
"os" "io/fs"
) )
// Options represents configurable options used to create client and server TLS configurations. // Options represents configurable options used to create client and server TLS configurations.
@ -40,7 +41,7 @@ func ClientConfig(opts Options) (cfg *tls.Config, err error) {
if opts.CertFile != "" || opts.KeyFile != "" { if opts.CertFile != "" || opts.KeyFile != "" {
if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil { if cert, err = CertFromFilePair(opts.CertFile, opts.KeyFile); err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("could not load x509 key pair (cert: %q, key: %q): %w", opts.CertFile, opts.KeyFile, err) return nil, fmt.Errorf("could not load x509 key pair (cert: %q, key: %q): %w", opts.CertFile, opts.KeyFile, err)
} }
return nil, fmt.Errorf("could not read x509 key pair (cert: %q, key: %q): %w", opts.CertFile, opts.KeyFile, err) return nil, fmt.Errorf("could not read x509 key pair (cert: %q, key: %q): %w", opts.CertFile, opts.KeyFile, err)

@ -22,6 +22,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"net/url" "net/url"
"os" "os"
"path" "path"
@ -634,7 +635,7 @@ func createOrOpenFile(filename string, append bool) (*os.File, error) {
func ensureDirectoryForFile(file string) error { func ensureDirectoryForFile(file string) error {
baseDir := path.Dir(file) baseDir := path.Dir(file)
_, err := os.Stat(baseDir) _, err := os.Stat(baseDir)
if err != nil && !os.IsNotExist(err) { if err != nil && !errors.Is(err, fs.ErrNotExist) {
return err return err
} }

@ -18,8 +18,10 @@ package action
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -626,7 +628,7 @@ func TestInstallReleaseOutputDir(t *testing.T) {
test.AssertGoldenFile(t, filepath.Join(dir, "hello/templates/rbac"), "rbac.txt") test.AssertGoldenFile(t, filepath.Join(dir, "hello/templates/rbac"), "rbac.txt")
_, err = os.Stat(filepath.Join(dir, "hello/templates/empty")) _, err = os.Stat(filepath.Join(dir, "hello/templates/empty"))
is.True(os.IsNotExist(err)) is.True(errors.Is(err, fs.ErrNotExist))
} }
func TestInstallOutputDirWithReleaseName(t *testing.T) { func TestInstallOutputDirWithReleaseName(t *testing.T) {
@ -662,7 +664,7 @@ func TestInstallOutputDirWithReleaseName(t *testing.T) {
test.AssertGoldenFile(t, filepath.Join(newDir, "hello/templates/rbac"), "rbac.txt") test.AssertGoldenFile(t, filepath.Join(newDir, "hello/templates/rbac"), "rbac.txt")
_, err = os.Stat(filepath.Join(newDir, "hello/templates/empty")) _, err = os.Stat(filepath.Join(newDir, "hello/templates/empty"))
is.True(os.IsNotExist(err)) is.True(errors.Is(err, fs.ErrNotExist))
} }
func TestNameAndChart(t *testing.T) { func TestNameAndChart(t *testing.T) {

@ -17,7 +17,6 @@ limitations under the License.
package action package action
import ( import (
"errors"
"fmt" "fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
@ -113,11 +112,7 @@ func checkOwnership(obj runtime.Object, releaseName, releaseNamespace string) er
} }
if len(errs) > 0 { if len(errs) > 0 {
err := errors.New("invalid ownership metadata") return fmt.Errorf("invalid ownership metadata; %w", joinErrors(errs, "; "))
for _, e := range errs {
err = fmt.Errorf("%w; %s", err, e)
}
return err
} }
return nil return nil

@ -17,7 +17,9 @@ limitations under the License.
package chartutil package chartutil
import ( import (
"errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -68,7 +70,7 @@ func IsChartDir(dirName string) (bool, error) {
} }
chartYaml := filepath.Join(dirName, ChartfileName) chartYaml := filepath.Join(dirName, ChartfileName)
if _, err := os.Stat(chartYaml); os.IsNotExist(err) { if _, err := os.Stat(chartYaml); errors.Is(err, fs.ErrNotExist) {
return false, fmt.Errorf("no %s exists in directory %q", ChartfileName, dirName) return false, fmt.Errorf("no %s exists in directory %q", ChartfileName, dirName)
} }

@ -22,6 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -112,7 +113,7 @@ func Save(c *chart.Chart, outDir string) (string, error) {
filename = filepath.Join(outDir, filename) filename = filepath.Join(outDir, filename)
dir := filepath.Dir(filename) dir := filepath.Dir(filename)
if stat, err := os.Stat(dir); err != nil { if stat, err := os.Stat(dir); err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
if err2 := os.MkdirAll(dir, 0755); err2 != nil { if err2 := os.MkdirAll(dir, 0755); err2 != nil {
return "", err2 return "", err2
} }

@ -165,8 +165,7 @@ func ToRenderValuesWithSchemaValidation(chrt *chart.Chart, chrtVals map[string]i
if !skipSchemaValidation { if !skipSchemaValidation {
if err := ValidateAgainstSchema(chrt, vals); err != nil { if err := ValidateAgainstSchema(chrt, vals); err != nil {
errFmt := "values don't meet the specifications of the schema(s) in the following chart(s):\n%s" return top, fmt.Errorf("values don't meet the specifications of the schema(s) in the following chart(s):\n%w", err)
return top, fmt.Errorf(errFmt, err.Error())
} }
} }

@ -21,6 +21,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
stdfs "io/fs"
"log" "log"
"net/url" "net/url"
"os" "os"
@ -253,7 +254,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error {
if !fi.IsDir() { if !fi.IsDir() {
return fmt.Errorf("%q is not a directory", destPath) return fmt.Errorf("%q is not a directory", destPath)
} }
} else if os.IsNotExist(err) { } else if errors.Is(err, stdfs.ErrNotExist) {
if err := os.MkdirAll(destPath, 0755); err != nil { if err := os.MkdirAll(destPath, 0755); err != nil {
return err return err
} }
@ -559,7 +560,7 @@ func (m *Manager) ensureMissingRepos(repoNames map[string]string, deps []*chart.
func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string, error) { func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string, error) {
rf, err := loadRepoConfig(m.RepositoryConfig) rf, err := loadRepoConfig(m.RepositoryConfig)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, stdfs.ErrNotExist) {
return make(map[string]string), nil return make(map[string]string), nil
} }
return nil, err return nil, err

@ -17,6 +17,8 @@ package downloader
import ( import (
"bytes" "bytes"
"errors"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -259,7 +261,7 @@ func TestDownloadAll(t *testing.T) {
t.Error(err) t.Error(err)
} }
if _, err := os.Stat(filepath.Join(chartPath, "charts", "signtest-0.1.0.tgz")); os.IsNotExist(err) { if _, err := os.Stat(filepath.Join(chartPath, "charts", "signtest-0.1.0.tgz")); errors.Is(err, fs.ErrNotExist) {
t.Error(err) t.Error(err)
} }

@ -20,7 +20,9 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io/fs"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -274,7 +276,7 @@ func TestExtract(t *testing.T) {
pluginYAMLFullPath := filepath.Join(tempDir, "plugin.yaml") pluginYAMLFullPath := filepath.Join(tempDir, "plugin.yaml")
if info, err := os.Stat(pluginYAMLFullPath); err != nil { if info, err := os.Stat(pluginYAMLFullPath); err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
t.Fatalf("Expected %s to exist but doesn't", pluginYAMLFullPath) t.Fatalf("Expected %s to exist but doesn't", pluginYAMLFullPath)
} }
t.Fatal(err) t.Fatal(err)
@ -284,7 +286,7 @@ func TestExtract(t *testing.T) {
readmeFullPath := filepath.Join(tempDir, "README.md") readmeFullPath := filepath.Join(tempDir, "README.md")
if info, err := os.Stat(readmeFullPath); err != nil { if info, err := os.Stat(readmeFullPath); err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
t.Fatalf("Expected %s to exist but doesn't", readmeFullPath) t.Fatalf("Expected %s to exist but doesn't", readmeFullPath)
} }
t.Fatal(err) t.Fatal(err)

@ -18,6 +18,7 @@ package installer // import "helm.sh/helm/v3/pkg/plugin/installer"
import ( import (
"errors" "errors"
"fmt" "fmt"
stdfs "io/fs"
"os" "os"
"sort" "sort"
@ -156,7 +157,7 @@ func (i *VCSInstaller) setVersion(repo vcs.Repo, ref string) error {
// sync will clone or update a remote repo. // sync will clone or update a remote repo.
func (i *VCSInstaller) sync(repo vcs.Repo) error { func (i *VCSInstaller) sync(repo vcs.Repo) error {
if _, err := os.Stat(repo.LocalPath()); os.IsNotExist(err) { if _, err := os.Stat(repo.LocalPath()); errors.Is(err, stdfs.ErrNotExist) {
debug("cloning %s to %s", repo.Remote(), repo.LocalPath()) debug("cloning %s to %s", repo.Remote(), repo.LocalPath())
return repo.Get() return repo.Get()
} }

@ -88,7 +88,7 @@ func getFullPath(binaryPath string) (string, error) {
// // The plugins variable can actually contain multiple paths, so loop through those // // The plugins variable can actually contain multiple paths, so loop through those
// for _, p := range filepath.SplitList(pluginDir) { // for _, p := range filepath.SplitList(pluginDir) {
// _, err := os.Stat(filepath.Join(p, binaryPath)) // _, err := os.Stat(filepath.Join(p, binaryPath))
// if err != nil && !os.IsNotExist(err) { // if err != nil && !errors.Is(err, fs.ErrNotExist) {
// return "", err // return "", err
// } else if err == nil { // } else if err == nil {
// binaryPath = filepath.Join(p, binaryPath) // binaryPath = filepath.Join(p, binaryPath)

@ -18,6 +18,7 @@ package pusher
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/fs"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -47,7 +48,7 @@ func (pusher *OCIPusher) Push(chartRef, href string, options ...Option) error {
func (pusher *OCIPusher) push(chartRef, href string) error { func (pusher *OCIPusher) push(chartRef, href string) error {
stat, err := os.Stat(chartRef) stat, err := os.Stat(chartRef)
if err != nil { if err != nil {
if os.IsNotExist(err) { if errors.Is(err, fs.ErrNotExist) {
return fmt.Errorf("%s: no such file", chartRef) return fmt.Errorf("%s: no such file", chartRef)
} }
return err return err

Loading…
Cancel
Save