From 4fa0826ff6401f5b65ad8ca1ee761fae930d7416 Mon Sep 17 00:00:00 2001 From: Adele Reed Date: Thu, 19 Dec 2024 12:28:49 -0800 Subject: [PATCH] Correct fmt.errorf usage --- cmd/copy.go | 2 +- cmd/copyEnumeratorInit.go | 2 +- cmd/jobsResume.go | 4 ++-- cmd/zc_traverser_file.go | 2 +- cmd/zc_traverser_local.go | 18 +++++++++--------- common/oauthTokenManager.go | 14 +++++++------- ste/JobPartPlanFileName.go | 4 ++-- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/cmd/copy.go b/cmd/copy.go index aeabba8e4..bdcefaba5 100644 --- a/cmd/copy.go +++ b/cmd/copy.go @@ -234,7 +234,7 @@ func (raw rawCopyCmdArgs) stripTrailingWildcardOnRemoteSource(location common.Lo gURLParts := common.NewGenericResourceURLParts(*resourceURL, location) if err != nil { - err = fmt.Errorf("failed to parse url %s; %s", result, err) + err = fmt.Errorf("failed to parse url %s; %w", result, err) return } diff --git a/cmd/copyEnumeratorInit.go b/cmd/copyEnumeratorInit.go index 73c537a34..724f62f70 100755 --- a/cmd/copyEnumeratorInit.go +++ b/cmd/copyEnumeratorInit.go @@ -168,7 +168,7 @@ func (cca *CookedCopyCmdArgs) initEnumerator(jobPartOrder common.CopyJobPartOrde containers, err := acctTraverser.listContainers() if err != nil { - return nil, fmt.Errorf("failed to list containers: %s", err) + return nil, fmt.Errorf("failed to list containers: %w", err) } // Resolve all container names up front. diff --git a/cmd/jobsResume.go b/cmd/jobsResume.go index 5d8b8acc7..72564f611 100644 --- a/cmd/jobsResume.go +++ b/cmd/jobsResume.go @@ -298,7 +298,7 @@ func (rca resumeCmdArgs) getSourceAndDestinationServiceClients( jobID, err := common.ParseJobID(rca.jobID) if err != nil { // Error for invalid JobId format - return nil, nil, fmt.Errorf("error parsing the jobId %s. Failed with error %s", rca.jobID, err.Error()) + return nil, nil, fmt.Errorf("error parsing the jobId %s. Failed with error %w", rca.jobID, err) } var getJobDetailsResponse common.GetJobDetailsResponse @@ -347,7 +347,7 @@ func (rca resumeCmdArgs) process() error { jobID, err := common.ParseJobID(rca.jobID) if err != nil { // If parsing gives an error, hence it is not a valid JobId format - return fmt.Errorf("error parsing the jobId %s. Failed with error %s", rca.jobID, err.Error()) + return fmt.Errorf("error parsing the jobId %s. Failed with error %w", rca.jobID, err) } includeTransfer := make(map[string]int) diff --git a/cmd/zc_traverser_file.go b/cmd/zc_traverser_file.go index 72af2de89..1221e4896 100644 --- a/cmd/zc_traverser_file.go +++ b/cmd/zc_traverser_file.go @@ -293,7 +293,7 @@ func (t *fileTraverser) Traverse(preprocessor objectMorpher, processor objectPro for pager.More() { lResp, err := pager.NextPage(t.ctx) if err != nil { - return fmt.Errorf("cannot list files due to reason %s", err) + return fmt.Errorf("cannot list files due to reason %w", err) } for _, fileInfo := range lResp.Segment.Files { if invalidBlobOrWindowsName(*fileInfo.Name) { diff --git a/cmd/zc_traverser_local.go b/cmd/zc_traverser_local.go index 7362104a7..c559308b3 100755 --- a/cmd/zc_traverser_local.go +++ b/cmd/zc_traverser_local.go @@ -287,7 +287,7 @@ func WalkWithSymlinks(appCtx context.Context, fullPath string, walkFunc filepath result, err := UnfurlSymlinks(filePath) if err != nil { - err = fmt.Errorf("Failed to resolve symlink %s: %s", filePath, err.Error()) + err = fmt.Errorf("failed to resolve symlink %s: %w", filePath, err) WarnStdoutAndScanningLog(err.Error()) writeToErrorChannel(errorChannel, ErrorFileInfo{FilePath: filePath, FileInfo: fileInfo, ErrorMsg: err}) return nil @@ -295,7 +295,7 @@ func WalkWithSymlinks(appCtx context.Context, fullPath string, walkFunc filepath result, err = filepath.Abs(result) if err != nil { - err = fmt.Errorf("Failed to get absolute path of symlink result %s: %s", filePath, err.Error()) + err = fmt.Errorf("failed to get absolute path of symlink result %s: %w", filePath, err) WarnStdoutAndScanningLog(err.Error()) writeToErrorChannel(errorChannel, ErrorFileInfo{FilePath: filePath, FileInfo: fileInfo, ErrorMsg: err}) return nil @@ -303,7 +303,7 @@ func WalkWithSymlinks(appCtx context.Context, fullPath string, walkFunc filepath slPath, err := filepath.Abs(filePath) if err != nil { - err = fmt.Errorf("Failed to get absolute path of %s: %s", filePath, err.Error()) + err = fmt.Errorf("failed to get absolute path of %s: %w", filePath, err) WarnStdoutAndScanningLog(err.Error()) writeToErrorChannel(errorChannel, ErrorFileInfo{FilePath: filePath, FileInfo: fileInfo, ErrorMsg: err}) return nil @@ -311,7 +311,7 @@ func WalkWithSymlinks(appCtx context.Context, fullPath string, walkFunc filepath rStat, err := os.Stat(result) if err != nil { - err = fmt.Errorf("Failed to get properties of symlink target at %s: %s", result, err.Error()) + err = fmt.Errorf("failed to get properties of symlink target at %s: %w", result, err) WarnStdoutAndScanningLog(err.Error()) writeToErrorChannel(errorChannel, ErrorFileInfo{FilePath: filePath, FileInfo: fileInfo, ErrorMsg: err}) return nil @@ -354,7 +354,7 @@ func WalkWithSymlinks(appCtx context.Context, fullPath string, walkFunc filepath result, err := filepath.Abs(filePath) if err != nil { - err = fmt.Errorf("Failed to get absolute path of %s: %s", filePath, err.Error()) + err = fmt.Errorf("failed to get absolute path of %s: %w", filePath, err) WarnStdoutAndScanningLog(err.Error()) writeToErrorChannel(errorChannel, ErrorFileInfo{FilePath: filePath, FileInfo: fileInfo, ErrorMsg: err}) return nil @@ -513,7 +513,7 @@ func (t *localTraverser) prepareHashingThreads(preprocessor objectMorpher, proce fullPath := filepath.Join(t.fullPath, relPath) fi, err := os.Stat(fullPath) // query LMT & if it's a directory if err != nil { - err = fmt.Errorf("failed to get properties of file result %s: %s", relPath, err.Error()) + err = fmt.Errorf("failed to get properties of file result %s: %w", relPath, err) hashError <- err return } @@ -524,7 +524,7 @@ func (t *localTraverser) prepareHashingThreads(preprocessor objectMorpher, proce f, err := os.OpenFile(fullPath, os.O_RDONLY, 0644) // perm is not used here since it's RO if err != nil { - err = fmt.Errorf("failed to open file for reading result %s: %s", relPath, err.Error()) + err = fmt.Errorf("failed to open file for reading result %s: %w", relPath, err) hashError <- err return } @@ -538,7 +538,7 @@ func (t *localTraverser) prepareHashingThreads(preprocessor objectMorpher, proce // hash.Hash provides a writer type, allowing us to do a (small, 32MB to be precise) buffered write into the hasher and avoid memory concerns _, err = io.Copy(hasher, f) if err != nil { - err = fmt.Errorf("failed to read file into hasher result %s: %s", relPath, err.Error()) + err = fmt.Errorf("failed to read file into hasher result %s: %w", relPath, err) hashError <- err return } @@ -644,7 +644,7 @@ func (t *localTraverser) Traverse(preprocessor objectMorpher, processor objectPr // it fails here if file does not exist if err != nil { azcopyScanningLogger.Log(common.LogError, fmt.Sprintf("Failed to scan path %s: %s", t.fullPath, err.Error())) - return fmt.Errorf("failed to scan path %s due to %s", t.fullPath, err.Error()) + return fmt.Errorf("failed to scan path %s due to %w", t.fullPath, err) } finalizer, hashingProcessor := t.prepareHashingThreads(preprocessor, processor, filters) diff --git a/common/oauthTokenManager.go b/common/oauthTokenManager.go index bd1b30181..2bb2f78e4 100644 --- a/common/oauthTokenManager.go +++ b/common/oauthTokenManager.go @@ -267,7 +267,7 @@ func (uotm *UserOAuthTokenManager) UserLogin(tenantID, activeDirectoryEndpoint s func (uotm *UserOAuthTokenManager) getCachedTokenInfo(ctx context.Context) (*OAuthTokenInfo, error) { hasToken, err := uotm.credCache.HasCachedToken() if err != nil { - return nil, fmt.Errorf("no cached token found, please log in with azcopy's login command, %v", err) + return nil, fmt.Errorf("no cached token found, please log in with azcopy's login command, %w", err) } if !hasToken { return nil, errors.New("no cached token found, please log in with azcopy's login command") @@ -275,12 +275,12 @@ func (uotm *UserOAuthTokenManager) getCachedTokenInfo(ctx context.Context) (*OAu tokenInfo, err := uotm.credCache.LoadToken() if err != nil { - return nil, fmt.Errorf("get cached token failed, %v", err) + return nil, fmt.Errorf("get cached token failed, %w", err) } freshToken, err := tokenInfo.Refresh(ctx) if err != nil { - return nil, fmt.Errorf("get cached token failed to ensure token fresh, please log in with azcopy's login command again, %v", err) + return nil, fmt.Errorf("get cached token failed to ensure token fresh, please log in with azcopy's login command again, %w", err) } // Update token cache, if token is updated. @@ -352,13 +352,13 @@ func (uotm *UserOAuthTokenManager) getTokenInfoFromEnvVar(ctx context.Context) ( tokenInfo, err := jsonToTokenInfo([]byte(rawToken)) if err != nil { - return nil, fmt.Errorf("get token from environment variable failed to unmarshal token, %v", err) + return nil, fmt.Errorf("get token from environment variable failed to unmarshal token, %w", err) } if tokenInfo.LoginType != EAutoLoginType.TokenStore() { refreshedToken, err := tokenInfo.Refresh(ctx) if err != nil { - return nil, fmt.Errorf("get token from environment variable failed to ensure token fresh, %v", err) + return nil, fmt.Errorf("get token from environment variable failed to ensure token fresh, %w", err) } tokenInfo.Token = *refreshedToken } @@ -541,12 +541,12 @@ func (tsc *TokenStoreCredential) GetToken(_ context.Context, _ policy.TokenReque defer tsc.lock.Unlock() hasToken, err := tokenStoreCredCache.HasCachedToken() if err != nil || !hasToken { - return azcore.AccessToken{}, fmt.Errorf("no cached token found in Token Store Mode(SE), %v", err) + return azcore.AccessToken{}, fmt.Errorf("no cached token found in Token Store Mode(SE), %w", err) } tokenInfo, err := tokenStoreCredCache.LoadToken() if err != nil { - return azcore.AccessToken{}, fmt.Errorf("get cached token failed in Token Store Mode(SE), %v", err) + return azcore.AccessToken{}, fmt.Errorf("get cached token failed in Token Store Mode(SE), %w", err) } tsc.token = &azcore.AccessToken{ diff --git a/ste/JobPartPlanFileName.go b/ste/JobPartPlanFileName.go index 18d8a0e41..30a7cf06a 100644 --- a/ste/JobPartPlanFileName.go +++ b/ste/JobPartPlanFileName.go @@ -46,7 +46,7 @@ func (jpfn JobPartPlanFileName) Parse() (jobID common.JobID, partNumber common.P jpfnSplit := strings.Split(string(jpfn), "--") jobId, err := common.ParseJobID(jpfnSplit[0]) if err != nil { - err = fmt.Errorf("failed to parse the JobId from JobPartFileName %s. Failed with error %s", string(jpfn), err.Error()) //nolint:staticcheck + err = fmt.Errorf("failed to parse the JobId from JobPartFileName %s. Failed with error %w", string(jpfn), err) //nolint:staticcheck // TODO: return here on error? or ignore } jobID = jobId @@ -144,7 +144,7 @@ func (jpfn JobPartPlanFileName) Create(order common.CopyJobPartOrderRequest) { // planPathname := planDir + "/" + string(jpfn) file, err := os.Create(jpfn.GetJobPartPlanPath()) if err != nil { - panic(fmt.Errorf("couldn't create job part plan file %q: %v", jpfn, err)) + panic(fmt.Errorf("couldn't create job part plan file %q: %w", jpfn, err)) } defer file.Close()