Skip to content

Commit

Permalink
Correct fmt.errorf usage
Browse files Browse the repository at this point in the history
  • Loading branch information
adreed-msft committed Dec 19, 2024
1 parent 9d7880f commit 4fa0826
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/copyEnumeratorInit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cmd/jobsResume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/zc_traverser_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions cmd/zc_traverser_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,31 +287,31 @@ 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
}

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
}

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
}

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions common/oauthTokenManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,20 @@ 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")
}

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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions ste/JobPartPlanFileName.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check failure on line 49 in ste/JobPartPlanFileName.go

View workflow job for this annotation

GitHub Actions / lint (1.23.1, ubuntu-latest)

ineffectual assignment to err (ineffassign)
// TODO: return here on error? or ignore
}
jobID = jobId
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 4fa0826

Please sign in to comment.