Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use %w to wrap error #475

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func (cerr *ChecksumError) Error() string {
func (c *FileChecksum) checksum(source string) error {
f, err := os.Open(source)
if err != nil {
return fmt.Errorf("Failed to open file for checksum: %s", err)
return fmt.Errorf("Failed to open file for checksum: %w", err)
}
defer f.Close()

c.Hash.Reset()
if _, err := io.Copy(c.Hash, f); err != nil {
return fmt.Errorf("Failed to hash: %s", err)
return fmt.Errorf("Failed to hash: %w", err)
}

if actual := c.Hash.Sum(nil); !bytes.Equal(actual, c.Value) {
Expand Down Expand Up @@ -128,7 +128,7 @@ func newChecksum(checksumValue, filename string) (*FileChecksum, error) {
var err error
c.Value, err = hex.DecodeString(checksumValue)
if err != nil {
return nil, fmt.Errorf("invalid checksum: %s", err)
return nil, fmt.Errorf("invalid checksum: %w", err)
}
return c, nil
}
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *Client) ChecksumFromFile(checksumFile string, src *url.URL) (*FileCheck
}
if err = c2.Get(); err != nil {
return nil, fmt.Errorf(
"Error downloading checksum file: %s", err)
"Error downloading checksum file: %w", err)
}

filename := filepath.Base(src.Path)
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (c *Client) Get() error {
td, err := ioutil.TempDir("", "getter")
if err != nil {
return fmt.Errorf(
"Error creating temporary directory for archive: %s", err)
"Error creating temporary directory for archive: %w", err)
}
defer os.RemoveAll(td)

Expand All @@ -220,7 +220,7 @@ func (c *Client) Get() error {
// Determine checksum if we have one
checksum, err := c.extractChecksum(u)
if err != nil {
return fmt.Errorf("invalid checksum: %s", err)
return fmt.Errorf("invalid checksum: %w", err)
}

// Delete the query parameter if we have it.
Expand Down Expand Up @@ -320,7 +320,7 @@ func (c *Client) Get() error {
// if we're specifying a subdir.
err := g.Get(dst, u)
if err != nil {
err = fmt.Errorf("error downloading '%s': %s", RedactURL(u), err)
err = fmt.Errorf("error downloading '%s': %w", RedactURL(u), err)
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion decompress_tgz.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (d *TarGzipDecompressor) Decompress(dst, src string, dir bool, umask os.Fil
// Gzip compression is second
gzipR, err := gzip.NewReader(f)
if err != nil {
return fmt.Errorf("Error opening a gzip reader for %s: %s", src, err)
return fmt.Errorf("Error opening a gzip reader for %s: %w", src, err)
}
defer gzipR.Close()

Expand Down
2 changes: 1 addition & 1 deletion decompress_txz.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *TarXzDecompressor) Decompress(dst, src string, dir bool, umask os.FileM
// xz compression is second
txzR, err := xz.NewReader(f)
if err != nil {
return fmt.Errorf("Error opening an xz reader for %s: %s", src, err)
return fmt.Errorf("Error opening an xz reader for %s: %w", src, err)
}

return untar(txzR, dst, src, dir, umask, d.FileSizeLimit, d.FilesLimit)
Expand Down
2 changes: 1 addition & 1 deletion decompress_tzst.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *TarZstdDecompressor) Decompress(dst, src string, dir bool, umask os.Fil
// Zstd compression is second
zstdR, err := zstd.NewReader(f)
if err != nil {
return fmt.Errorf("Error opening a zstd reader for %s: %s", src, err)
return fmt.Errorf("Error opening a zstd reader for %s: %w", src, err)
}
defer zstdR.Close()

Expand Down
2 changes: 1 addition & 1 deletion detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Detect(src string, pwd string, ds []Detector) (string, error) {
if subDir != "" {
u, err := url.Parse(result)
if err != nil {
return "", fmt.Errorf("Error parsing URL: %s", err)
return "", fmt.Errorf("Error parsing URL: %w", err)
}
u.Path += "//" + subDir

Expand Down
6 changes: 3 additions & 3 deletions detect_bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (d *BitBucketDetector) Detect(src, _ string) (string, bool, error) {
func (d *BitBucketDetector) detectHTTP(src string) (string, bool, error) {
u, err := url.Parse("https://" + src)
if err != nil {
return "", true, fmt.Errorf("error parsing BitBucket URL: %s", err)
return "", true, fmt.Errorf("error parsing BitBucket URL: %w", err)
}

// We need to get info on this BitBucket repository to determine whether
Expand All @@ -38,7 +38,7 @@ func (d *BitBucketDetector) detectHTTP(src string) (string, bool, error) {
infoUrl := "https://api.bitbucket.org/2.0/repositories" + u.Path
resp, err := http.Get(infoUrl)
if err != nil {
return "", true, fmt.Errorf("error looking up BitBucket URL: %s", err)
return "", true, fmt.Errorf("error looking up BitBucket URL: %w", err)
}
if resp.StatusCode == 403 {
// A private repo
Expand All @@ -48,7 +48,7 @@ func (d *BitBucketDetector) detectHTTP(src string) (string, bool, error) {
}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&info); err != nil {
return "", true, fmt.Errorf("error looking up BitBucket URL: %s", err)
return "", true, fmt.Errorf("error looking up BitBucket URL: %w", err)
}

switch info.SCM {
Expand Down
2 changes: 1 addition & 1 deletion detect_gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (d *GCSDetector) detectHTTP(src string) (string, bool, error) {
url, err := url.Parse(fmt.Sprintf("https://www.googleapis.com/storage/%s/%s/%s",
version, bucket, object))
if err != nil {
return "", false, fmt.Errorf("error parsing GCS URL: %s", err)
return "", false, fmt.Errorf("error parsing GCS URL: %w", err)
}

return "gcs::" + url.String(), true, nil
Expand Down
2 changes: 1 addition & 1 deletion detect_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) {
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", true, fmt.Errorf("error parsing GitHub URL: %s", err)
return "", true, fmt.Errorf("error parsing GitHub URL: %w", err)
}

if !strings.HasSuffix(url.Path, ".git") {
Expand Down
2 changes: 1 addition & 1 deletion detect_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (d *GitLabDetector) detectHTTP(src string) (string, bool, error) {
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
repoUrl, err := url.Parse(urlStr)
if err != nil {
return "", true, fmt.Errorf("error parsing GitLab URL: %s", err)
return "", true, fmt.Errorf("error parsing GitLab URL: %w", err)
}

if !strings.HasSuffix(repoUrl.Path, ".git") {
Expand Down
6 changes: 3 additions & 3 deletions detect_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (d *S3Detector) detectPathStyle(region string, parts []string) (string, boo
urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s", region, strings.Join(parts, "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
return "", false, fmt.Errorf("error parsing S3 URL: %w", err)
}

return "s3::" + url.String(), true, nil
Expand All @@ -56,7 +56,7 @@ func (d *S3Detector) detectVhostStyle(region, bucket string, parts []string) (st
urlStr := fmt.Sprintf("https://%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
return "", false, fmt.Errorf("error parsing S3 URL: %w", err)
}

return "s3::" + url.String(), true, nil
Expand All @@ -66,7 +66,7 @@ func (d *S3Detector) detectNewVhostStyle(region, bucket string, parts []string)
urlStr := fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/"))
url, err := url.Parse(urlStr)
if err != nil {
return "", false, fmt.Errorf("error parsing S3 URL: %s", err)
return "", false, fmt.Errorf("error parsing S3 URL: %w", err)
}

return "s3::" + url.String(), true, nil
Expand Down
2 changes: 1 addition & 1 deletion detect_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func detectSSH(src string) (*url.URL, error) {
if qidx < len(path) {
q, err := url.ParseQuery(path[qidx+1:])
if err != nil {
return nil, fmt.Errorf("error parsing GitHub SSH URL: %s", err)
return nil, fmt.Errorf("error parsing GitHub SSH URL: %w", err)
}
u.RawQuery = q.Encode()
}
Expand Down
2 changes: 1 addition & 1 deletion folder_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (s *FolderStorage) Get(key string, source string, update bool) error {
} else if !os.IsNotExist(err) {
// If the error we got wasn't a file-not-exist error, then
// something went wrong and we should report it.
return fmt.Errorf("Error reading module directory: %s", err)
return fmt.Errorf("Error reading module directory: %w", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions get_file_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (g *FileGetter) Get(dst string, u *url.URL) error {

// The source path must exist and be a directory to be usable.
if fi, err := os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
return fmt.Errorf("source path error: %w", err)
} else if !fi.IsDir() {
return fmt.Errorf("source path must be a directory")
}
Expand Down Expand Up @@ -59,7 +59,7 @@ func (g *FileGetter) GetFile(dst string, u *url.URL) error {
var fi os.FileInfo
var err error
if fi, err = os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
return fmt.Errorf("source path error: %w", err)
} else if fi.IsDir() {
return fmt.Errorf("source path must be a file")
}
Expand Down
4 changes: 2 additions & 2 deletions get_file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (g *FileGetter) Get(dst string, u *url.URL) error {

// The source path must exist and be a directory to be usable.
if fi, err := os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
return fmt.Errorf("source path error: %w", err)
} else if !fi.IsDir() {
return fmt.Errorf("source path must be a directory")
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func (g *FileGetter) GetFile(dst string, u *url.URL) error {

// The source path must exist and be a directory to be usable.
if fi, err := os.Stat(path); err != nil {
return fmt.Errorf("source path error: %s", err)
return fmt.Errorf("source path error: %w", err)
} else if fi.IsDir() {
return fmt.Errorf("source path must be a file")
}
Expand Down
2 changes: 1 addition & 1 deletion netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func addAuthFromNetrc(u *url.URL) error {
// Load up the netrc file
net, err := netrc.ParseFile(path)
if err != nil {
return fmt.Errorf("Error parsing netrc file at %q: %s", path, err)
return fmt.Errorf("Error parsing netrc file at %q: %w", path, err)
}

machine := net.FindMachine(u.Host)
Expand Down