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

Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) #21551

Merged
merged 12 commits into from
Oct 24, 2022

Conversation

delvh
Copy link
Member

@delvh delvh commented Oct 22, 2022

Found using
find . -type f -name '*.go' -print -exec vim {} -c ':%s/fmt\.Errorf(\(.*\)%v\(.*\)err/fmt.Errorf(\1%w\2err/g' -c ':wq' \;

models/git/branches.go Outdated Show resolved Hide resolved
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Oct 22, 2022
@delvh delvh marked this pull request as draft October 22, 2022 16:58
@delvh delvh changed the title Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) WIP: Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) Oct 22, 2022
@silverwind
Copy link
Member

silverwind commented Oct 22, 2022

IIRC, golangci-lint may have a linter to detect this. Not sure I find it now.

@delvh
Copy link
Member Author

delvh commented Oct 22, 2022

Agreed. But please only once this PR is merged.
In the next few minutes, a force-push will come that takes care of handling multiple errors and no error at all.
(force-push as I won't revert the commit simply to run the other search, and instead use the state before the commit)
Surprisingly, I seem to have a found a simple regex that also takes care of the edge cases I know of.

Found using `find . -type f -name '*.go' -print -exec vim {} -c ':%s/fmt\.Errorf(\(.*\)%v\(.*\)err/fmt.Errorf(\1%w\2err/g' -c ':wq' \;`
@delvh delvh force-pushed the maintenance/w-instead-of-v-for-errorf branch from bbd2286 to 653678e Compare October 22, 2022 17:21
@delvh delvh changed the title WIP: Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) Oct 22, 2022
@delvh delvh marked this pull request as ready for review October 22, 2022 17:24
@zeripath
Copy link
Contributor

Oh gods there are some delightfully unenlightening error messages there.

@wxiaoguang I think you're right we're just going to have to go down the route of adding the stack trace to these.

@delvh
Copy link
Member Author

delvh commented Oct 22, 2022

What exactly do you mean?

@silverwind
Copy link
Member

silverwind commented Oct 22, 2022

Found the linter: https://github.com/polyfloyd/go-errorlint

You can just add errorlint to .golangci.yml after all errors are fixed and the linter will ensure no new issues arise. I'd do it in this PR.

@6543 6543 added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Oct 22, 2022
@zeripath
Copy link
Contributor

@delvh not really related to this PR but look at some of the errors reported, e.g. in notification.go:loadRepo(...):

	if n.Repository == nil {
		n.Repository, err = repo_model.GetRepositoryByIDCtx(ctx, n.RepoID)
		if err != nil {
			return fmt.Errorf("getRepositoryByID [%d]: %w", n.RepoID, err) // <- BUT WHY WAS I TRYING TO GET THIS?
		}
	}

If we think about this message a bit more - The error that GetRepositoryByIDCtx should return should already be GetRepositoryByIDCtx[%d]: %w and we shouldn't have to be adding that here. The error returned by this function should be adding the context that it's from loadRepo().

We're just adding the wrong context and doing so at every level.

I should add that at least this error is giving the context of the RepoID that is failing to be got from the DB. Many don't even provide that kind of information, e.g.:

// DeleteGPGKey deletes GPG key information in database.
func DeleteGPGKey(doer *user_model.User, id int64) (err error) {
	key, err := GetGPGKeyByID(id)
	if err != nil {
		if IsErrGPGKeyNotExist(err) {
			return nil
		}
		return fmt.Errorf("GetPublicKeyByID: %w", err) // WHICH BLOODY KEY?
	}

@6543
Copy link
Member

6543 commented Oct 22, 2022

I did test errorlint enabled and it catched a lot of good issues ... - but to many to enable it within this pull

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Oct 22, 2022
Copy link
Member

@6543 6543 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as comment

@delvh delvh requested a review from 6543 October 22, 2022 18:08
models/migrations/v210.go Outdated Show resolved Hide resolved
@zeripath
Copy link
Contributor

 ❯ git diff main  | grep '%w.*%w'                                                             [19:44:36]
+						return fmt.Errorf("unable to check webauthn_credential[lower_name: %s, user_id:%w]. Error: %w", remigrated.LowerName, remigrated.UserID, err)
+			return fmt.Errorf("failed to rollback directory change during failed username change from: %s to: %s. DB Error: %w. Filesystem Error: %w", oldUserName, newUserName, err, err2)
+				err = fmt.Errorf("%w %w", err2, err)
+			return fmt.Errorf("%w other names: %w", err, l.MultiChannelledLog.GetEventLoggerNames())

Signed-off-by: Andrew Thornton <art27@cantab.net>
@GiteaBot GiteaBot removed the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Oct 22, 2022
@GiteaBot GiteaBot added the lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. label Oct 22, 2022
@wxiaoguang
Copy link
Contributor

Generally L-G-T-M.

CI failures are related, golangci-lint does very good job and catches mismatched formatting arguments.

@zeripath
Copy link
Contributor

There was an incorrect whitespace change in blame_test.go.

@delvh
Copy link
Member Author

delvh commented Oct 24, 2022

What happened to the CI?
Why are there no longer any CI icons?

@zeripath zeripath merged commit 0ebb45c into go-gitea:main Oct 24, 2022
@delvh delvh deleted the maintenance/w-instead-of-v-for-errorf branch October 24, 2022 19:49
zjjhot added a commit to zjjhot/gitea that referenced this pull request Oct 25, 2022
* upstream/main:
  [skip ci] Updated translations via Crowdin
  Add sqlite vscode extension to Gitpod configuration (go-gitea#21552)
  Replace all instances of fmt.Errorf(%v) with fmt.Errorf(%w) (go-gitea#21551)
  Fix package access for admins and inactive users (go-gitea#21580)
  Allow for resolution of NPM registry paths that match upstream (go-gitea#21568)
  Added missing headers on user packages page (go-gitea#21172)
  Record OAuth client type at registration (go-gitea#21316)
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants