Skip to content

Commit 0ebb45c

Browse files
delvh6543zeripathwxiaoguang
authored
Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) (#21551)
Found using `find . -type f -name '*.go' -print -exec vim {} -c ':%s/fmt\.Errorf(\(.*\)%v\(.*\)err/fmt.Errorf(\1%w\2err/g' -c ':wq' \;` Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Andrew Thornton <art27@cantab.net> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent 7c11a73 commit 0ebb45c

File tree

207 files changed

+857
-857
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+857
-857
lines changed

cmd/admin.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ func runCreateUser(c *cli.Context) error {
588588
}
589589

590590
if err := user_model.CreateUser(u, overwriteDefault); err != nil {
591-
return fmt.Errorf("CreateUser: %v", err)
591+
return fmt.Errorf("CreateUser: %w", err)
592592
}
593593

594594
if c.Bool("access-token") {
@@ -735,7 +735,7 @@ func runRepoSyncReleases(_ *cli.Context) error {
735735
Private: true,
736736
})
737737
if err != nil {
738-
return fmt.Errorf("SearchRepositoryByName: %v", err)
738+
return fmt.Errorf("SearchRepositoryByName: %w", err)
739739
}
740740
if len(repos) == 0 {
741741
break

cmd/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Ensure you are running in the correct environment or set the correct configurati
6868
If this is the intended configuration file complete the [database] section.`, setting.CustomConf)
6969
}
7070
if err := db.InitEngine(ctx); err != nil {
71-
return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %v", setting.CustomConf, err)
71+
return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %w", setting.CustomConf, err)
7272
}
7373
return nil
7474
}

cmd/dump_repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func runDumpRepository(ctx *cli.Context) error {
166166
// make sure the directory doesn't exist or is empty, prevent from deleting user files
167167
repoDir := ctx.String("repo_dir")
168168
if exists, err := util.IsExist(repoDir); err != nil {
169-
return fmt.Errorf("unable to stat repo_dir %q: %v", repoDir, err)
169+
return fmt.Errorf("unable to stat repo_dir %q: %w", repoDir, err)
170170
} else if exists {
171171
if isDir, _ := util.IsDir(repoDir); !isDir {
172172
return fmt.Errorf("repo_dir %q already exists but it's not a directory", repoDir)

cmd/embedded.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ func runViewDo(c *cli.Context) error {
186186

187187
data, err := assets[0].Section.Asset(assets[0].Name)
188188
if err != nil {
189-
return fmt.Errorf("%s: %v", assets[0].Path, err)
189+
return fmt.Errorf("%s: %w", assets[0].Path, err)
190190
}
191191

192192
if _, err = os.Stdout.Write(data); err != nil {
193-
return fmt.Errorf("%s: %v", assets[0].Path, err)
193+
return fmt.Errorf("%s: %w", assets[0].Path, err)
194194
}
195195

196196
return nil
@@ -251,19 +251,19 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
251251

252252
data, err := a.Section.Asset(a.Name)
253253
if err != nil {
254-
return fmt.Errorf("%s: %v", a.Path, err)
254+
return fmt.Errorf("%s: %w", a.Path, err)
255255
}
256256

257257
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
258-
return fmt.Errorf("%s: %v", dir, err)
258+
return fmt.Errorf("%s: %w", dir, err)
259259
}
260260

261261
perms := os.ModePerm & 0o666
262262

263263
fi, err := os.Lstat(dest)
264264
if err != nil {
265265
if !errors.Is(err, os.ErrNotExist) {
266-
return fmt.Errorf("%s: %v", dest, err)
266+
return fmt.Errorf("%s: %w", dest, err)
267267
}
268268
} else if !overwrite && !rename {
269269
fmt.Printf("%s already exists; skipped.\n", dest)
@@ -272,20 +272,20 @@ func extractAsset(d string, a asset, overwrite, rename bool) error {
272272
return fmt.Errorf("%s already exists, but it's not a regular file", dest)
273273
} else if rename {
274274
if err := util.Rename(dest, dest+".bak"); err != nil {
275-
return fmt.Errorf("Error creating backup for %s: %v", dest, err)
275+
return fmt.Errorf("Error creating backup for %s: %w", dest, err)
276276
}
277277
// Attempt to respect file permissions mask (even if user:group will be set anew)
278278
perms = fi.Mode()
279279
}
280280

281281
file, err := os.OpenFile(dest, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, perms)
282282
if err != nil {
283-
return fmt.Errorf("%s: %v", dest, err)
283+
return fmt.Errorf("%s: %w", dest, err)
284284
}
285285
defer file.Close()
286286

287287
if _, err = file.Write(data); err != nil {
288-
return fmt.Errorf("%s: %v", dest, err)
288+
return fmt.Errorf("%s: %w", dest, err)
289289
}
290290

291291
fmt.Println(dest)
@@ -325,7 +325,7 @@ func getPatterns(args []string) ([]glob.Glob, error) {
325325
pat := make([]glob.Glob, len(args))
326326
for i := range args {
327327
if g, err := glob.Compile(args[i], '/'); err != nil {
328-
return nil, fmt.Errorf("'%s': Invalid glob pattern: %v", args[i], err)
328+
return nil, fmt.Errorf("'%s': Invalid glob pattern: %w", args[i], err)
329329
} else {
330330
pat[i] = g
331331
}

cmd/hook.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func runHookPostReceive(c *cli.Context) error {
312312

313313
// First of all run update-server-info no matter what
314314
if _, _, err := git.NewCommand(ctx, "update-server-info").RunStdString(nil); err != nil {
315-
return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
315+
return fmt.Errorf("Failed to call 'git update-server-info': %w", err)
316316
}
317317

318318
// Now if we're an internal don't do anything else

models/activities/action.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,11 @@ func GetFeeds(ctx context.Context, opts GetFeedsOptions) (ActionList, error) {
359359
actions := make([]*Action, 0, opts.PageSize)
360360

361361
if err := sess.Desc("`action`.created_unix").Find(&actions); err != nil {
362-
return nil, fmt.Errorf("Find: %v", err)
362+
return nil, fmt.Errorf("Find: %w", err)
363363
}
364364

365365
if err := ActionList(actions).loadAttributes(ctx); err != nil {
366-
return nil, fmt.Errorf("LoadAttributes: %v", err)
366+
return nil, fmt.Errorf("LoadAttributes: %w", err)
367367
}
368368

369369
return actions, nil
@@ -415,7 +415,7 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
415415
env := organization.OrgFromUser(opts.RequestedUser).AccessibleTeamReposEnv(opts.RequestedTeam)
416416
teamRepoIDs, err := env.RepoIDs(1, opts.RequestedUser.NumRepos)
417417
if err != nil {
418-
return nil, fmt.Errorf("GetTeamRepositories: %v", err)
418+
return nil, fmt.Errorf("GetTeamRepositories: %w", err)
419419
}
420420
cond = cond.And(builder.In("repo_id", teamRepoIDs))
421421
}
@@ -477,14 +477,14 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
477477
// Add feeds for user self and all watchers.
478478
watchers, err = repo_model.GetWatchers(ctx, act.RepoID)
479479
if err != nil {
480-
return fmt.Errorf("get watchers: %v", err)
480+
return fmt.Errorf("get watchers: %w", err)
481481
}
482482
}
483483

484484
// Add feed for actioner.
485485
act.UserID = act.ActUserID
486486
if _, err = e.Insert(act); err != nil {
487-
return fmt.Errorf("insert new actioner: %v", err)
487+
return fmt.Errorf("insert new actioner: %w", err)
488488
}
489489

490490
if repoChanged {
@@ -493,7 +493,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
493493

494494
// check repo owner exist.
495495
if err := act.Repo.GetOwner(ctx); err != nil {
496-
return fmt.Errorf("can't get repo owner: %v", err)
496+
return fmt.Errorf("can't get repo owner: %w", err)
497497
}
498498
} else if act.Repo == nil {
499499
act.Repo = repo
@@ -504,7 +504,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
504504
act.ID = 0
505505
act.UserID = act.Repo.Owner.ID
506506
if err = db.Insert(ctx, act); err != nil {
507-
return fmt.Errorf("insert new actioner: %v", err)
507+
return fmt.Errorf("insert new actioner: %w", err)
508508
}
509509
}
510510

@@ -557,7 +557,7 @@ func notifyWatchers(ctx context.Context, actions ...*Action) error {
557557
}
558558

559559
if err = db.Insert(ctx, act); err != nil {
560-
return fmt.Errorf("insert new action: %v", err)
560+
return fmt.Errorf("insert new action: %w", err)
561561
}
562562
}
563563
}

models/activities/action_list.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (actions ActionList) loadUsers(ctx context.Context) (map[int64]*user_model.
3636
In("id", userIDs).
3737
Find(&userMaps)
3838
if err != nil {
39-
return nil, fmt.Errorf("find user: %v", err)
39+
return nil, fmt.Errorf("find user: %w", err)
4040
}
4141

4242
for _, action := range actions {
@@ -62,7 +62,7 @@ func (actions ActionList) loadRepositories(ctx context.Context) error {
6262
repoMaps := make(map[int64]*repo_model.Repository, len(repoIDs))
6363
err := db.GetEngine(ctx).In("id", repoIDs).Find(&repoMaps)
6464
if err != nil {
65-
return fmt.Errorf("find repository: %v", err)
65+
return fmt.Errorf("find repository: %w", err)
6666
}
6767

6868
for _, action := range actions {

models/activities/notification.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (n *Notification) loadRepo(ctx context.Context) (err error) {
403403
if n.Repository == nil {
404404
n.Repository, err = repo_model.GetRepositoryByIDCtx(ctx, n.RepoID)
405405
if err != nil {
406-
return fmt.Errorf("getRepositoryByID [%d]: %v", n.RepoID, err)
406+
return fmt.Errorf("getRepositoryByID [%d]: %w", n.RepoID, err)
407407
}
408408
}
409409
return nil
@@ -413,7 +413,7 @@ func (n *Notification) loadIssue(ctx context.Context) (err error) {
413413
if n.Issue == nil && n.IssueID != 0 {
414414
n.Issue, err = issues_model.GetIssueByID(ctx, n.IssueID)
415415
if err != nil {
416-
return fmt.Errorf("getIssueByID [%d]: %v", n.IssueID, err)
416+
return fmt.Errorf("getIssueByID [%d]: %w", n.IssueID, err)
417417
}
418418
return n.Issue.LoadAttributes(ctx)
419419
}
@@ -440,7 +440,7 @@ func (n *Notification) loadUser(ctx context.Context) (err error) {
440440
if n.User == nil {
441441
n.User, err = user_model.GetUserByIDCtx(ctx, n.UserID)
442442
if err != nil {
443-
return fmt.Errorf("getUserByID [%d]: %v", n.UserID, err)
443+
return fmt.Errorf("getUserByID [%d]: %w", n.UserID, err)
444444
}
445445
}
446446
return nil

models/activities/repo_activity.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,32 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom
4949
stats := &ActivityStats{Code: &git.CodeActivityStats{}}
5050
if releases {
5151
if err := stats.FillReleases(repo.ID, timeFrom); err != nil {
52-
return nil, fmt.Errorf("FillReleases: %v", err)
52+
return nil, fmt.Errorf("FillReleases: %w", err)
5353
}
5454
}
5555
if prs {
5656
if err := stats.FillPullRequests(repo.ID, timeFrom); err != nil {
57-
return nil, fmt.Errorf("FillPullRequests: %v", err)
57+
return nil, fmt.Errorf("FillPullRequests: %w", err)
5858
}
5959
}
6060
if issues {
6161
if err := stats.FillIssues(repo.ID, timeFrom); err != nil {
62-
return nil, fmt.Errorf("FillIssues: %v", err)
62+
return nil, fmt.Errorf("FillIssues: %w", err)
6363
}
6464
}
6565
if err := stats.FillUnresolvedIssues(repo.ID, timeFrom, issues, prs); err != nil {
66-
return nil, fmt.Errorf("FillUnresolvedIssues: %v", err)
66+
return nil, fmt.Errorf("FillUnresolvedIssues: %w", err)
6767
}
6868
if code {
6969
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
7070
if err != nil {
71-
return nil, fmt.Errorf("OpenRepository: %v", err)
71+
return nil, fmt.Errorf("OpenRepository: %w", err)
7272
}
7373
defer closer.Close()
7474

7575
code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch)
7676
if err != nil {
77-
return nil, fmt.Errorf("FillFromGit: %v", err)
77+
return nil, fmt.Errorf("FillFromGit: %w", err)
7878
}
7979
stats.Code = code
8080
}
@@ -85,13 +85,13 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom
8585
func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) {
8686
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
8787
if err != nil {
88-
return nil, fmt.Errorf("OpenRepository: %v", err)
88+
return nil, fmt.Errorf("OpenRepository: %w", err)
8989
}
9090
defer closer.Close()
9191

9292
code, err := gitRepo.GetCodeActivityStats(timeFrom, "")
9393
if err != nil {
94-
return nil, fmt.Errorf("FillFromGit: %v", err)
94+
return nil, fmt.Errorf("FillFromGit: %w", err)
9595
}
9696
if code.Authors == nil {
9797
return nil, nil

models/asymkey/gpg_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
226226
if IsErrGPGKeyNotExist(err) {
227227
return nil
228228
}
229-
return fmt.Errorf("GetPublicKeyByID: %v", err)
229+
return fmt.Errorf("GetPublicKeyByID: %w", err)
230230
}
231231

232232
// Check if user has access to delete this key.

models/asymkey/ssh_key.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub
130130
LoginSourceID: authSourceID,
131131
}
132132
if err = addKey(ctx, key); err != nil {
133-
return nil, fmt.Errorf("addKey: %v", err)
133+
return nil, fmt.Errorf("addKey: %w", err)
134134
}
135135

136136
return key, committer.Commit()

models/asymkey/ssh_key_deploy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func AddDeployKey(repoID int64, name, content string, readOnly bool) (*DeployKey
151151
pkey.Content = content
152152
pkey.Name = name
153153
if err = addKey(ctx, pkey); err != nil {
154-
return nil, fmt.Errorf("addKey: %v", err)
154+
return nil, fmt.Errorf("addKey: %w", err)
155155
}
156156
}
157157

models/asymkey/ssh_key_fingerprint.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func CalcFingerprint(publicKeyContent string) (string, error) {
9595
log.Info("%s", publicKeyContent)
9696
return "", err
9797
}
98-
return "", fmt.Errorf("%s: %v", fnName, err)
98+
return "", fmt.Errorf("%s: %w", fnName, err)
9999
}
100100
return fp, nil
101101
}

0 commit comments

Comments
 (0)