Skip to content

Commit e46ab91

Browse files
authored
Merge pull request #1836 from benmoss/goerr113
🌱 Fix error handling for goerr113 linter
2 parents ee8ea85 + c1feda4 commit e46ab91

File tree

20 files changed

+87
-86
lines changed

20 files changed

+87
-86
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ linters:
1212
- gocognit
1313
- gomnd
1414
- godot
15-
- goerr113
1615
- gofumpt
1716
# Run with --fast=false for more extensive checks
1817
fast: true

api/v1alpha3/awsmachinetemplate_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ limitations under the License.
1717
package v1alpha3
1818

1919
import (
20-
"errors"
2120
"reflect"
2221

22+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2323
"k8s.io/apimachinery/pkg/runtime"
2424
"k8s.io/apimachinery/pkg/util/validation/field"
2525
ctrl "sigs.k8s.io/controller-runtime"
@@ -60,7 +60,7 @@ func (r *AWSMachineTemplate) ValidateCreate() error {
6060
func (r *AWSMachineTemplate) ValidateUpdate(old runtime.Object) error {
6161
oldAWSMachineTemplate := old.(*AWSMachineTemplate)
6262
if !reflect.DeepEqual(r.Spec, oldAWSMachineTemplate.Spec) {
63-
return errors.New("awsMachineTemplateSpec is immutable")
63+
return apierrors.NewBadRequest("AWSMachineTemplate.Spec is immutable")
6464
}
6565

6666
return nil

cmd/clusterawsadm/cloudformation/bootstrap/iam.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func (t Template) GenerateManagedIAMPolicyDocuments(policyDocDir string) error {
4747

4848
pds, err := converters.IAMPolicyDocumentToJSON(*pd)
4949
if err != nil {
50-
return fmt.Errorf("failed to marshal policy document for ManagedIAMPolicy %q: %v", pn, err)
50+
return fmt.Errorf("failed to marshal policy document for ManagedIAMPolicy %q: %w", pn, err)
5151
}
5252

5353
fn := path.Join(policyDocDir, fmt.Sprintf("%s.json", pn))
5454
err = ioutil.WriteFile(fn, []byte(pds), 0o600)
5555
if err != nil {
56-
return fmt.Errorf("failed to generate policy document for ManagedIAMPolicy %q: %v", pn, err)
56+
return fmt.Errorf("failed to generate policy document for ManagedIAMPolicy %q: %w", pn, err)
5757
}
5858
}
5959
return nil

cmd/clusterawsadm/cmd/alpha/bootstrap/bootstrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func generateIAMPolicyDocJSON() *cobra.Command {
206206
t := bootstrapTemplateFromCmdLine()
207207
err := t.GenerateManagedIAMPolicyDocuments(policyDocDir)
208208
if err != nil {
209-
return fmt.Errorf("failed to generate PolicyDocument for all ManagedIAMPolicies: %v", err)
209+
return fmt.Errorf("failed to generate PolicyDocument for all ManagedIAMPolicies: %w", err)
210210
}
211211

212212
fmt.Printf("PolicyDocument for all ManagedIAMPolicies successfully generated in JSON at %q\n", policyDocDir)

cmd/clusterawsadm/cmd/bootstrap/credentials/credentials.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ const (
7676
`
7777
)
7878

79+
var errInvalidOutputFlag = errors.New("invalid output flag. Expected rawSharedConfig or base64SharedConfig")
80+
7981
// RootCmd is the root of the `alpha bootstrap command`
8082
func RootCmd() *cobra.Command {
8183
newCmd := &cobra.Command{
@@ -103,7 +105,7 @@ func getOutputFlag(cmd *cobra.Command) (string, error) {
103105
case rawSharedConfig, base64SharedConfig:
104106
return val, nil
105107
default:
106-
return "", errors.New("invalid output flag. Expected rawSharedConfig or base64SharedConfig")
108+
return "", errInvalidOutputFlag
107109
}
108110
}
109111

cmd/clusterawsadm/cmd/bootstrap/iam/iam_doc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
2626
)
2727

28+
var errInvalidDocumentName = fmt.Errorf("invalid document name, use one of: %+v", bootstrap.ManagedIAMPolicyNames)
29+
2830
func printPolicyCmd() *cobra.Command {
2931
newCmd := &cobra.Command{
3032
Use: "print-policy",
@@ -76,7 +78,7 @@ func printPolicyCmd() *cobra.Command {
7678
func getDocumentName(cmd *cobra.Command) (bootstrap.PolicyName, error) {
7779
val := bootstrap.PolicyName(cmd.Flags().Lookup("document").Value.String())
7880
if !val.IsValid() {
79-
return "", fmt.Errorf("invalid document name, use one of: %+v", bootstrap.ManagedIAMPolicyNames)
81+
return "", errInvalidDocumentName
8082
}
8183

8284
return val, nil

cmd/clusterawsadm/configreader/configreader.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,24 @@ import (
2828
bootstrapschemev1 "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/api/bootstrap/v1alpha1/scheme"
2929
)
3030

31+
type errEmptyBootstrapConfig string
32+
33+
func (e errEmptyBootstrapConfig) Error() string {
34+
return fmt.Sprintf("bootstrap config file %q was empty", string(e))
35+
}
36+
3137
// LoadConfigFile loads a YAML file representing a bootstrapv1.AWSIAMConfiguration.
3238
func LoadConfigFile(name string) (*bootstrapv1.AWSIAMConfiguration, error) {
33-
const errFmt = "failed to load bootstrap config file %s, error %v"
3439
// compute absolute path based on current working dir
3540
iamConfigFile, err := filepath.Abs(name)
3641
if err != nil {
37-
return nil, fmt.Errorf(errFmt, name, err)
42+
return nil, fmt.Errorf("failed to convert IAM config path into absolute path %s, error: %w", name, err)
3843
}
3944
loader, err := newFsLoader(iamConfigFile)
4045
if err != nil {
41-
return nil, fmt.Errorf(errFmt, name, err)
42-
}
43-
kc, err := loader.Load()
44-
if err != nil {
45-
return nil, fmt.Errorf(errFmt, name, err)
46+
return nil, fmt.Errorf("failed to initialize filesystem loader: %w", err)
4647
}
47-
return kc, err
48+
return loader.Load()
4849
}
4950

5051
// Loader loads configuration from a storage layer.
@@ -83,12 +84,12 @@ func newFsLoader(bootstrapFile string) (loader, error) {
8384
func (loader *fsLoader) Load() (*bootstrapv1.AWSIAMConfiguration, error) {
8485
data, err := loader.ReadFile(loader.bootstrapFile)
8586
if err != nil {
86-
return nil, fmt.Errorf("failed to read bootstrap config file %q, error: %v", loader.bootstrapFile, err)
87+
return nil, fmt.Errorf("failed to read bootstrap config file %q, error: %w", loader.bootstrapFile, err)
8788
}
8889

8990
// no configuration is an error, some parameters are required
9091
if len(data) == 0 {
91-
return nil, fmt.Errorf("bootstrap config file %q was empty", loader.bootstrapFile)
92+
return nil, errEmptyBootstrapConfig(loader.bootstrapFile)
9293
}
9394

9495
kc, err := DecodeBootstrapConfiguration(loader.bootstrapCodecs, data)

cmd/clusterawsadm/credentials/credentials.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ aws_session_token = {{ .SessionToken }}
4141

4242
const AWSDefaultRegion = "us-east-1"
4343

44+
var ErrNoAWSRegionConfigured = errors.New("no AWS region configured. Use --region or set AWS_REGION or DEFAULT_AWS_REGION environment variable")
45+
46+
type ErrEnvironmentVariableNotFound string
47+
48+
func (e ErrEnvironmentVariableNotFound) Error() string {
49+
return fmt.Sprintf("environment variable %q not found", string(e))
50+
}
51+
4452
type AWSCredentials struct {
4553
AccessKeyID string
4654
SecretAccessKey string
@@ -78,13 +86,13 @@ func ResolveRegion(explicitRegion string) (string, error) {
7886
if err == nil {
7987
return region, nil
8088
}
81-
return "", errors.New("no AWS region configured. Use --region or set AWS_REGION or DEFAULT_AWS_REGION environment variable")
89+
return "", ErrNoAWSRegionConfigured
8290
}
8391

8492
func getEnv(key string) (string, error) {
8593
val, ok := os.LookupEnv(key)
8694
if !ok {
87-
return "", fmt.Errorf("environment variable %q not found", key)
95+
return "", ErrEnvironmentVariableNotFound(key)
8896
}
8997
return val, nil
9098
}

controllers/awsmachine_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ func (r *AWSMachineReconciler) reconcileDelete(machineScope *scope.MachineScope,
384384
func (r *AWSMachineReconciler) findInstance(scope *scope.MachineScope, ec2svc services.EC2MachineInterface) (*infrav1.Instance, error) {
385385
// Parse the ProviderID.
386386
pid, err := noderefutil.NewProviderID(scope.GetProviderID())
387-
if err != nil && err != noderefutil.ErrEmptyProviderID {
387+
if err != nil && !errors.Is(err, noderefutil.ErrEmptyProviderID) {
388388
return nil, errors.Wrapf(err, "failed to parse Spec.ProviderID")
389389
}
390390

pkg/cloud/awserrors/errors.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,28 @@ func Message(err error) string {
6363

6464
// EC2Error is an error exposed to users of this library.
6565
type EC2Error struct {
66-
err error
66+
msg string
6767

6868
Code int
6969
}
7070

7171
// Error implements the Error interface.
7272
func (e *EC2Error) Error() string {
73-
return e.err.Error()
73+
return e.msg
7474
}
7575

76-
// NewNotFound returns a new error which indicates that the resource of the kind and the name was not found.
77-
func NewNotFound(err error) error {
76+
// NewNotFound returns an error which indicates that the resource of the kind and the name was not found.
77+
func NewNotFound(msg string) error {
7878
return &EC2Error{
79-
err: err,
79+
msg: msg,
8080
Code: http.StatusNotFound,
8181
}
8282
}
8383

84-
// NewConflict returns a new error which indicates that the request cannot be processed due to a conflict.
85-
func NewConflict(err error) error {
84+
// NewConflict returns an error which indicates that the request cannot be processed due to a conflict.
85+
func NewConflict(msg string) error {
8686
return &EC2Error{
87-
err: err,
87+
msg: msg,
8888
Code: http.StatusConflict,
8989
}
9090
}
@@ -96,10 +96,10 @@ func IsResourceExists(err error) bool {
9696
return false
9797
}
9898

99-
// NewFailedDependency returns a new error which indicates that a dependency failure status
100-
func NewFailedDependency(err error) error {
99+
// NewFailedDependency returns an error which indicates that a dependency failure status
100+
func NewFailedDependency(msg string) error {
101101
return &EC2Error{
102-
err: err,
102+
msg: msg,
103103
Code: http.StatusFailedDependency,
104104
}
105105
}

0 commit comments

Comments
 (0)