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

awsproviderlint: New Check: Prefer %w verb for *awserr.Error in returning errors with context #12991

Closed
breathingdust opened this issue Apr 24, 2020 · 4 comments
Labels
linter Pertains to changes to or issues with the various linters. provider Pertains to the provider itself, rather than any interaction with AWS. technical-debt Addresses areas of the codebase that need refactoring or redesign.

Comments

@breathingdust
Copy link
Member

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

Go 1.13 introduced a new error formatting verb, %w, which allows the original error to be wrapped and checked by type in consumers, rather than just checking by value.

Previously, it was not (easily) possible to have an AWS Go SDK helper function return a contextual message along the original error, e.g. this would not work

func doSomethingCool(conn *ec2.EC2) error {
  input := &ec2.DescribeSecurityGroupsInput{}
  _, err := conn.DescribeSecurityGroups(input)
  
  if err != nil {
    return fmt.Errorf("error describing EC2 Security Groups: %s", err)
  }

  return nil
}

// calling reference

err := doSomethingCool(conn)

// this would not work previously and the Go build chain will not raise any issues (error is checked by value)
if isAWSErr(err, "Code", "Message") {
  // error handling
}

Now with the %w verb in the AWS Go SDK calling function and the now updated isAWSErr() helper function, the error checking can work as expected.

Flagged Code

fmt.Errorf("oh no: %s", err)

Passing Code

fmt.Errorf("oh no: %w", err)

References

@breathingdust breathingdust added technical-debt Addresses areas of the codebase that need refactoring or redesign. provider Pertains to the provider itself, rather than any interaction with AWS. labels Apr 24, 2020
@ewbankkit

This comment has been minimized.

@bflad bflad added the linter Pertains to changes to or issues with the various linters. label Oct 28, 2020
ewbankkit added a commit to farhanangullia/terraform-provider-aws that referenced this issue Aug 11, 2021
ewbankkit added a commit to nijave/terraform-provider-aws that referenced this issue Sep 21, 2021
@ewbankkit
Copy link
Contributor

ewbankkit commented May 10, 2022

We should be able to use the golangci-lint errorlint linter for this.

We may want to wait until after #15090 is complete before tackling this one as CRUD handler functions will then return diag.Diagnostics, not error.

@ewbankkit
Copy link
Contributor

Superseded by #15891.

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
linter Pertains to changes to or issues with the various linters. provider Pertains to the provider itself, rather than any interaction with AWS. technical-debt Addresses areas of the codebase that need refactoring or redesign.
Projects
None yet
Development

No branches or pull requests

3 participants