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

ci(lint): add errorlint #1561

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ linters:
- unconvert
- unused
- stylecheck
- errorlint

issues:
exclude:
Expand Down
14 changes: 6 additions & 8 deletions internal/schemautil/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schemautil

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -586,7 +587,7 @@ func getDefaultDiskSpaceIfNotSet(ctx context.Context, d *schema.ResourceData, cl
if aiven.IsNotFound(err) {
return 0, nil
}
return 0, fmt.Errorf("unable to get service plan parameters: %s", err)
return 0, fmt.Errorf("unable to get service plan parameters: %w", err)
}

if ads, ok := d.GetOk("additional_disk_space"); ok {
Expand Down Expand Up @@ -743,7 +744,7 @@ func copyServicePropertiesFromAPIResponseToTerraform(
}

if err := d.Set(serviceType+"_user_config", newUserConfig); err != nil {
return fmt.Errorf("cannot set `%s_user_config` : %s; Please make sure that all Aiven services have unique s names", serviceType, err)
return fmt.Errorf("cannot set `%s_user_config` : %w; Please make sure that all Aiven services have unique s names", serviceType, err)
}

params := s.URIParams
Expand Down Expand Up @@ -784,7 +785,7 @@ func copyServicePropertiesFromAPIResponseToTerraform(
}

if err := d.Set("components", FlattenServiceComponents(s)); err != nil {
return fmt.Errorf("cannot set `components` : %s", err)
return fmt.Errorf("cannot set `components` : %w", err)
}

return copyConnectionInfoFromAPIResponseToTerraform(d, serviceType, s.ConnectionInfo, s.Metadata)
Expand Down Expand Up @@ -872,11 +873,8 @@ func copyConnectionInfoFromAPIResponseToTerraform(
// IsUnknownRole checks if the database returned an error because of an unknown role
// to make deletions idempotent
func IsUnknownRole(err error) bool {
aivenError, ok := err.(aiven.Error)
if !ok {
return false
}
return strings.Contains(aivenError.Message, "Code: 511")
var e *aiven.Error
return errors.As(err, &e) && strings.Contains(e.Message, "Code: 511")
}

// IsUnknownResource is a function to handle errors that we want to treat as "Not Found"
Expand Down
5 changes: 3 additions & 2 deletions internal/sdkprovider/kafkatopicrepository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kafkatopicrepository

import (
"context"
"errors"
"fmt"
"strings"

Expand All @@ -26,7 +27,7 @@ func (rep *repository) Create(ctx context.Context, project, service string, req
}

// If this is not errNotFound, then something happened
if err != errNotFound {
if !errors.Is(err, errNotFound) {
return err
}

Expand Down Expand Up @@ -59,7 +60,7 @@ func (rep *repository) Create(ctx context.Context, project, service string, req
// Retry lib returns a custom error object
// we can't compare in tests with
if err != nil {
return fmt.Errorf("topic create error: %s", err)
return fmt.Errorf("topic create error: %w", err)
}
return err
}
10 changes: 8 additions & 2 deletions internal/sdkprovider/kafkatopicrepository/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kafkatopicrepository

import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
Expand All @@ -24,7 +25,7 @@ func TestCreateConflict(t *testing.T) {
wg.Add(1)
go func() {
err := rep.Create(ctx, "a", "b", aiven.CreateKafkaTopicRequest{TopicName: "c"})
if err == errAlreadyExists {
if errors.Is(err, errAlreadyExists) {
atomic.AddInt32(&conflictErr, 1)
}
wg.Done()
Expand Down Expand Up @@ -113,7 +114,12 @@ func TestCreateRetries(t *testing.T) {
TopicName: "my-topic",
}
err := rep.Create(ctx, "foo", "bar", req)
assert.Equal(t, opt.expectErr, err)
// Check the error message using EqualError because the error is wrapped
if opt.expectErr == nil {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, opt.expectErr.Error())
}
assert.Equal(t, opt.expectCalled, client.createCalled)
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/sdkprovider/kafkatopicrepository/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (rep *repository) fetch(ctx context.Context, queue map[string]*request) {
if err != nil {
// Send errors
// Flattens error to a string, because it might go really completed for testing
err = fmt.Errorf("topic read error: %s", err)
err = fmt.Errorf("topic read error: %w", err)
for _, r := range reqs {
r.send(nil, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ func TestRepositoryRead(t *testing.T) {
defer wg.Done()
topic, err := rep.Read(ctx, opt.requests[i].project, opt.requests[i].service, opt.requests[i].topic)
assert.Equal(t, opt.responses[i].topic, topic)
assert.Equal(t, opt.responses[i].err, err)
// Check the error message using EqualError because the error is wrapped
if opt.responses[i].err == nil {
assert.NoError(t, err)
} else {
assert.EqualError(t, err, opt.responses[i].err.Error())
}
}(i)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account_test

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -87,7 +88,8 @@ func testAccCheckAivenAccountTeamMemberResourceDestroy(s *terraform.State) error

r, err := c.Accounts.List(ctx)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand All @@ -98,7 +100,8 @@ func testAccCheckAivenAccountTeamMemberResourceDestroy(s *terraform.State) error
if a.Id == accountID {
ri, err := c.AccountTeamInvites.List(ctx, accountID, teamID)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account_test

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -93,7 +94,8 @@ func testAccCheckAivenAccountTeamProjectResourceDestroy(s *terraform.State) erro

r, err := c.Accounts.List(ctx)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand All @@ -104,7 +106,8 @@ func testAccCheckAivenAccountTeamProjectResourceDestroy(s *terraform.State) erro
if a.Id == accountID {
rp, err := c.AccountTeamProjects.List(ctx, accountID, teamID)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand Down
7 changes: 5 additions & 2 deletions internal/sdkprovider/service/account/account_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account_test

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -79,7 +80,8 @@ func testAccCheckAivenAccountTeamResourceDestroy(s *terraform.State) error {

r, err := c.Accounts.List(ctx)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand All @@ -90,7 +92,8 @@ func testAccCheckAivenAccountTeamResourceDestroy(s *terraform.State) error {
if ac.Id == accountID {
rl, err := c.AccountTeams.List(ctx, accountID)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand Down
4 changes: 3 additions & 1 deletion internal/sdkprovider/service/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account_test

import (
"context"
"errors"
"fmt"
"log"
"testing"
Expand Down Expand Up @@ -87,7 +88,8 @@ func testAccCheckAivenAccountResourceDestroy(s *terraform.State) error {

r, err := c.Accounts.List(ctx)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}

Expand Down
24 changes: 13 additions & 11 deletions internal/sdkprovider/service/account/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package account

import (
"context"
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -119,7 +120,8 @@ func sweepAccounts(ctx context.Context) func(region string) error {

for _, a := range accounts {
if err := client.Accounts.Delete(ctx, a.Id); err != nil {
if err.(aiven.Error).Status == 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status == 404 {
continue
}

Expand Down Expand Up @@ -172,40 +174,40 @@ func sweepAccountTeamMembers(ctx context.Context) func(region string) error {

accounts, err := listTestAccounts(ctx)
if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %s", err)
return fmt.Errorf("error retrieving a list of accounts : %w", err)
}

for _, a := range accounts {
tr, err := client.AccountTeams.List(ctx, a.Id)
if err != nil {
return fmt.Errorf("error retrieving a list of account teams : %s", err)
return fmt.Errorf("error retrieving a list of account teams : %w", err)
}

for _, t := range tr.Teams {
if strings.Contains(t.Name, "test-acc-team-") {
// delete all account team invitations
mi, err := client.AccountTeamInvites.List(ctx, t.AccountId, t.Id)
if err != nil {
return fmt.Errorf("error retrieving a list of account team invitations : %s", err)
return fmt.Errorf("error retrieving a list of account team invitations : %w", err)
}

for _, i := range mi.Invites {
err := client.AccountTeamInvites.Delete(ctx, i.AccountId, i.TeamId, i.UserEmail)
if err != nil {
return fmt.Errorf("cannot delete account team invitation : %s", err)
return fmt.Errorf("cannot delete account team invitation : %w", err)
}
}

// delete all account team members
mr, err := client.AccountTeamMembers.List(ctx, t.AccountId, t.Id)
if err != nil {
return fmt.Errorf("error retrieving a list of account team members : %s", err)
return fmt.Errorf("error retrieving a list of account team members : %w", err)
}

for _, m := range mr.Members {
err := client.AccountTeamMembers.Delete(ctx, t.AccountId, t.Id, m.UserId)
if err != nil {
return fmt.Errorf("cannot delete account team member : %s", err)
return fmt.Errorf("cannot delete account team member : %w", err)
}
}
}
Expand All @@ -226,26 +228,26 @@ func sweepAccountTeamProjects(ctx context.Context) func(region string) error {

accounts, err := listTestAccounts(ctx)
if err != nil {
return fmt.Errorf("error retrieving a list of accounts : %s", err)
return fmt.Errorf("error retrieving a list of accounts : %w", err)
}

for _, a := range accounts {
tr, err := client.AccountTeams.List(ctx, a.Id)
if err != nil {
return fmt.Errorf("error retrieving a list of account teams : %s", err)
return fmt.Errorf("error retrieving a list of account teams : %w", err)
}

for _, t := range tr.Teams {
if strings.Contains(t.Name, "test-acc-team-") {
pr, err := client.AccountTeamProjects.List(ctx, t.AccountId, t.Id)
if err != nil {
return fmt.Errorf("error retrieving a list of account team projects : %s", err)
return fmt.Errorf("error retrieving a list of account team projects : %w", err)
}

for _, p := range pr.Projects {
err := client.AccountTeamProjects.Delete(ctx, t.AccountId, t.Id, p.ProjectName)
if err != nil {
return fmt.Errorf("cannot delete account team project : %s", err)
return fmt.Errorf("cannot delete account team project : %w", err)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cassandra_test

import (
"context"
"errors"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -56,7 +57,8 @@ func testAccCheckAivenCassandraUserResourceDestroy(s *terraform.State) error {

p, err := c.ServiceUsers.Get(ctx, projectName, serviceName, username)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse_test

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -59,7 +60,8 @@ func testAccCheckAivenClickhouseUserResourceDestroy(s *terraform.State) error {

p, err := c.ClickhouseUser.Get(ctx, projectName, serviceName, uuid)
if err != nil {
if err.(aiven.Error).Status != 404 {
var e *aiven.Error
if errors.As(err, &e) && e.Status != 404 {
return err
}
}
Expand Down
8 changes: 3 additions & 5 deletions internal/sdkprovider/service/clickhouse/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clickhouse

import (
"context"
"errors"
"fmt"
"log"
"strings"
Expand All @@ -10,11 +11,8 @@ import (
)

func isUnknownRole(err error) bool {
aivenError, ok := err.(aiven.Error)
if !ok {
return false
}
return strings.Contains(aivenError.Message, "Code: 511")
var e *aiven.Error
return errors.As(err, &e) && strings.Contains(e.Message, "Code: 511")
}

func CreateRole(ctx context.Context, client *aiven.Client, projectName, serviceName, roleName string) error {
Expand Down
Loading
Loading