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

Update error usage patterns to go1.13+ #7529

Merged
merged 3 commits into from
Oct 14, 2020
Merged

Conversation

farazdagi
Copy link
Contributor

What type of PR is this?

Other / Cleanup

What does this PR do? Why is it needed?

  • In Golang 1.13 extra method for wrapping errors (see https://golang.org/pkg/errors/ for details) has been introduced.
  • So, it is generally preferred, to use errors.Is(err, errType) instead of err == errType when comparing non-nil errors to some type (as former is able to unwrap wrapped errors and compare correctly).
  • Additionally, instead of passing the original error in conventional fmt.Errorf("sth wrong: %v", err), it is better to wrap the error using new verb %w (just %v -> %w is enough i.e. fmt.Errorf("sth wrong: %w", err)), as that way client code can get access to the original error (unwrap) if necessary.

Which issues(s) does this PR fix?

N/A

Other notes for review

  • IMP: Since we are using github.com/pkg/errors to wrap errors, there are relatively few places with fmt.Errorf(). Existing code, which relies on that custom package, is not touched. For future code, however, if stack trace is not required it is probably worth considering using fmt.Errorf("custom msg: %w", err) to wrap errors.

@farazdagi farazdagi added the Cleanup Code health! label Oct 14, 2020
@farazdagi farazdagi self-assigned this Oct 14, 2020
@farazdagi farazdagi marked this pull request as ready for review October 14, 2020 16:42
@farazdagi farazdagi requested a review from a team as a code owner October 14, 2020 16:42
@farazdagi farazdagi added OK to merge Ready For Review A pull request ready for code review labels Oct 14, 2020
}
sTime, err := mathutil.Add64(genesisTimeSec, timeSinceGenesis)
if err != nil {
return time.Unix(0, 0), fmt.Errorf("slot (%d) is in the far distant future: %v", slot, err)
return time.Unix(0, 0), fmt.Errorf("slot (%d) is in the far distant future: %w", slot, err)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we use errors.Wrapf?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • The only difference is that stack traces are included with errors.Wrapf.
  • Original code didn't use the errors.Wrapf(), so I am not sure whether it should be used or not. The scope of this PR was just to make sure if we are including original error, include it with %w i.e. wrap using new verb available in Golang 1.13

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, thanks!

@prylabs-bulldozer prylabs-bulldozer bot merged commit a005c77 into master Oct 14, 2020
@delete-merged-branch delete-merged-branch bot deleted the go1.13-errors branch October 14, 2020 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cleanup Code health! Ready For Review A pull request ready for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants