Skip to content

Commit 588b99b

Browse files
authored
Merge pull request #192 from hakkiai/fix/multi-error-returns
Refactor: use errors.Join for multi-error returns
2 parents 16d3a46 + bcb1d2d commit 588b99b

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

controller/internal/reconciler/bucketaccess.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func getAllBucketClaims(
306306
}
307307

308308
if len(errs) > 0 {
309-
return nil, fmt.Errorf("could not get one or more BucketClaims: %v", errs)
309+
return nil, fmt.Errorf("could not get one or more BucketClaims: %w", errors.Join(errs...))
310310
}
311311

312312
if len(claims) != len(claimAccesses) {
@@ -345,7 +345,7 @@ func markAllBucketClaimsAsAccessed(
345345
}
346346
}
347347
if len(errs) > 0 {
348-
return fmt.Errorf("failed to mark one or more BucketClaims as having a BucketAccess reference: %v", errs)
348+
return fmt.Errorf("failed to mark one or more BucketClaims as having a BucketAccess reference: %w", errors.Join(errs...))
349349
}
350350

351351
return nil
@@ -356,28 +356,28 @@ func validateAccessAgainstClass(
356356
class *cosiapi.BucketAccessClassSpec,
357357
access *cosiapi.BucketAccessSpec,
358358
) error {
359-
errs := []string{}
359+
errs := []error{}
360360

361361
needServiceAccount := class.AuthenticationType == cosiapi.BucketAccessAuthenticationTypeServiceAccount
362362
if needServiceAccount && access.ServiceAccountName == "" {
363-
errs = append(errs, "serviceAccountName must be specified")
363+
errs = append(errs, fmt.Errorf("serviceAccountName must be specified"))
364364
}
365365

366366
if class.FeatureOptions.DisallowMultiBucketAccess && len(access.BucketClaims) > 1 {
367-
errs = append(errs, "multi-bucket access is disallowed")
367+
errs = append(errs, fmt.Errorf("multi-bucket access is disallowed"))
368368
}
369369

370370
for _, claimRef := range access.BucketClaims {
371371
if slices.Contains(class.FeatureOptions.DisallowedBucketAccessModes, claimRef.AccessMode) {
372372
errs = append(errs,
373-
fmt.Sprintf("accessMode %q requested for BucketClaim %q is disallowed",
373+
fmt.Errorf("accessMode %q requested for BucketClaim %q is disallowed",
374374
claimRef.AccessMode, claimRef.BucketClaimName),
375375
)
376376
}
377377
}
378378

379379
if len(errs) > 0 {
380-
return fmt.Errorf("one or more features are disallowed by the BucketAccessClass: %v", errs)
380+
return fmt.Errorf("one or more features are disallowed by the BucketAccessClass: %w", errors.Join(errs...))
381381
}
382382
return nil
383383
}

internal/protocol/azure.go

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

1919
import (
20+
"errors"
2021
"fmt"
2122

2223
cosiapi "sigs.k8s.io/container-object-storage-interface/client/apis/objectstorage/v1alpha2"
@@ -60,15 +61,15 @@ func (AzureBucketInfoTranslator) ApiToRpc(vars map[cosiapi.BucketInfoVar]string)
6061
func (AzureBucketInfoTranslator) Validate(
6162
vars map[cosiapi.BucketInfoVar]string, _ cosiapi.BucketAccessAuthenticationType,
6263
) error {
63-
errs := []string{}
64+
errs := []error{}
6465

6566
storageAccount := vars[cosiapi.BucketInfoVar_Azure_StorageAccount]
6667
if storageAccount == "" {
67-
errs = append(errs, "azure storage account cannot be unset")
68+
errs = append(errs, fmt.Errorf("azure storage account cannot be unset"))
6869
}
6970

7071
if len(errs) > 0 {
71-
return fmt.Errorf("azure bucket info is invalid: %v", errs)
72+
return fmt.Errorf("azure bucket info is invalid: %w", errors.Join(errs...))
7273
}
7374
return nil
7475
}
@@ -107,15 +108,15 @@ func (AzureCredentialTranslator) Validate(
107108
return nil
108109
}
109110

110-
errs := []string{}
111+
errs := []error{}
111112

112113
accessToken := vars[cosiapi.CredentialVar_Azure_AccessToken]
113114
if accessToken == "" {
114-
errs = append(errs, "azure access token cannot be unset")
115+
errs = append(errs, fmt.Errorf("azure access token cannot be unset"))
115116
}
116117

117118
if len(errs) > 0 {
118-
return fmt.Errorf("azure credential info is invalid: %v", errs)
119+
return fmt.Errorf("azure credential info is invalid: %w", errors.Join(errs...))
119120
}
120121
return nil
121122
}

internal/protocol/gcs.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package protocol
1818

1919
import (
20+
"errors"
2021
"fmt"
2122

2223
cosiapi "sigs.k8s.io/container-object-storage-interface/client/apis/objectstorage/v1alpha2"
@@ -62,20 +63,20 @@ func (GcsBucketInfoTranslator) ApiToRpc(vars map[cosiapi.BucketInfoVar]string) *
6263
func (GcsBucketInfoTranslator) Validate(
6364
vars map[cosiapi.BucketInfoVar]string, _ cosiapi.BucketAccessAuthenticationType,
6465
) error {
65-
errs := []string{}
66+
errs := []error{}
6667

6768
bucketName := vars[cosiapi.BucketInfoVar_GCS_BucketName]
6869
if bucketName == "" {
69-
errs = append(errs, "GCS bucket name cannot be unset")
70+
errs = append(errs, fmt.Errorf("GCS bucket name cannot be unset"))
7071
}
7172

7273
projectId := vars[cosiapi.BucketInfoVar_GCS_ProjectId]
7374
if projectId == "" {
74-
errs = append(errs, "GCS project ID cannot be unset")
75+
errs = append(errs, fmt.Errorf("GCS project ID cannot be unset"))
7576
}
7677

7778
if len(errs) > 0 {
78-
return fmt.Errorf("GCS bucket info is invalid: %v", errs)
79+
return fmt.Errorf("GCS bucket info is invalid: %w", errors.Join(errs...))
7980
}
8081
return nil
8182
}
@@ -113,34 +114,34 @@ func (GcsCredentialTranslator) ApiToRpc(vars map[cosiapi.CredentialVar]string) *
113114
func (GcsCredentialTranslator) Validate(
114115
vars map[cosiapi.CredentialVar]string, authType cosiapi.BucketAccessAuthenticationType,
115116
) error {
116-
errs := []string{}
117+
errs := []error{}
117118

118119
switch authType {
119120
case cosiapi.BucketAccessAuthenticationTypeKey:
120121
accessId := vars[cosiapi.CredentialVar_GCS_AccessId]
121122
if accessId == "" {
122-
errs = append(errs, "GCS access ID cannot be unset")
123+
errs = append(errs, fmt.Errorf("GCS access ID cannot be unset"))
123124
}
124125

125126
accessSecret := vars[cosiapi.CredentialVar_GCS_AccessSecret]
126127
if accessSecret == "" {
127-
errs = append(errs, "GCS access secret cannot be unset")
128+
errs = append(errs, fmt.Errorf("GCS access secret cannot be unset"))
128129
}
129130

130131
case cosiapi.BucketAccessAuthenticationTypeServiceAccount:
131132
privateKeyName := vars[cosiapi.CredentialVar_GCS_PrivateKeyName]
132133
if privateKeyName == "" {
133-
errs = append(errs, "GCS private key name cannot be unset")
134+
errs = append(errs, fmt.Errorf("GCS private key name cannot be unset"))
134135
}
135136

136137
serviceAccount := vars[cosiapi.CredentialVar_GCS_ServiceAccount]
137138
if serviceAccount == "" {
138-
errs = append(errs, "GCS service account cannot be unset")
139+
errs = append(errs, fmt.Errorf("GCS service account cannot be unset"))
139140
}
140141
}
141142

142143
if len(errs) > 0 {
143-
return fmt.Errorf("GCS credential info is invalid: %v", errs)
144+
return fmt.Errorf("GCS credential info is invalid: %w", errors.Join(errs...))
144145
}
145146
return nil
146147
}

internal/protocol/s3.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package protocol
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"slices"
2223

@@ -97,30 +98,30 @@ func (S3BucketInfoTranslator) ApiToRpc(vars map[cosiapi.BucketInfoVar]string) *c
9798
func (S3BucketInfoTranslator) Validate(
9899
vars map[cosiapi.BucketInfoVar]string, _ cosiapi.BucketAccessAuthenticationType,
99100
) error {
100-
errs := []string{}
101+
errs := []error{}
101102

102103
id := vars[cosiapi.BucketInfoVar_S3_BucketId]
103104
if id == "" {
104-
errs = append(errs, "S3 bucket ID cannot be unset")
105+
errs = append(errs, fmt.Errorf("S3 bucket ID cannot be unset"))
105106
}
106107

107108
ep := vars[cosiapi.BucketInfoVar_S3_Endpoint]
108109
if ep == "" {
109-
errs = append(errs, "S3 endpoint cannot be unset")
110+
errs = append(errs, fmt.Errorf("S3 endpoint cannot be unset"))
110111
}
111112

112113
rg := vars[cosiapi.BucketInfoVar_S3_Region]
113114
if rg == "" {
114-
errs = append(errs, "S3 region cannot be unset")
115+
errs = append(errs, fmt.Errorf("S3 region cannot be unset"))
115116
}
116117

117118
as := vars[cosiapi.BucketInfoVar_S3_AddressingStyle]
118119
if !slices.Contains(validS3AddressingStyles, as) {
119-
errs = append(errs, fmt.Sprintf("S3 addressing style %q must be one of %v", as, validS3AddressingStyles))
120+
errs = append(errs, fmt.Errorf("S3 addressing style %q must be one of %v", as, validS3AddressingStyles))
120121
}
121122

122123
if len(errs) > 0 {
123-
return fmt.Errorf("S3 bucket info is invalid: %v", errs)
124+
return fmt.Errorf("S3 bucket info is invalid: %w", errors.Join(errs...))
124125
}
125126
return nil
126127
}
@@ -159,20 +160,20 @@ func (S3CredentialTranslator) Validate(
159160
return nil
160161
}
161162

162-
errs := []string{}
163+
errs := []error{}
163164

164165
accessKeyId := vars[cosiapi.CredentialVar_S3_AccessKeyId]
165166
if accessKeyId == "" {
166-
errs = append(errs, "S3 access key ID cannot be unset")
167+
errs = append(errs, fmt.Errorf("S3 access key ID cannot be unset"))
167168
}
168169

169170
accessSecretKey := vars[cosiapi.CredentialVar_S3_AccessSecretKey]
170171
if accessSecretKey == "" {
171-
errs = append(errs, "S3 access secret key cannot be unset")
172+
errs = append(errs, fmt.Errorf("S3 access secret key cannot be unset"))
172173
}
173174

174175
if len(errs) > 0 {
175-
return fmt.Errorf("S3 credential info is invalid: %v", errs)
176+
return fmt.Errorf("S3 credential info is invalid: %w", errors.Join(errs...))
176177
}
177178
return nil
178179
}

sidecar/internal/reconciler/bucket.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,13 @@ func parseProtocolBucketInfo(pbi *cosiproto.ObjectProtocolAndBucketInfo) (
317317

318318
// convert an API proto list into an RPC proto message list
319319
func objectProtocolListFromApiList(apiList []cosiapi.ObjectProtocol) ([]*cosiproto.ObjectProtocol, error) {
320-
errs := []string{}
320+
errs := []error{}
321321
out := []*cosiproto.ObjectProtocol{}
322322

323323
for _, apiProto := range apiList {
324324
rpcProto, err := protocol.ObjectProtocolTranslator{}.ApiToRpc(apiProto)
325325
if err != nil {
326-
errs = append(errs, err.Error())
326+
errs = append(errs, err)
327327
continue
328328
}
329329
out = append(out, &cosiproto.ObjectProtocol{
@@ -332,7 +332,7 @@ func objectProtocolListFromApiList(apiList []cosiapi.ObjectProtocol) ([]*cosipro
332332
}
333333

334334
if len(errs) > 0 {
335-
return nil, fmt.Errorf("failed to parse protocol list: %v", errs)
335+
return nil, fmt.Errorf("failed to parse protocol list: %w", errors.Join(errs...))
336336
}
337337
return out, nil
338338
}
@@ -362,7 +362,7 @@ func validateBucketSupportsProtocols(supported, required []cosiapi.ObjectProtoco
362362
}
363363
}
364364
if len(unsupported) > 0 {
365-
return fmt.Errorf("required protocols are not supported: %v", required)
365+
return fmt.Errorf("required protocols are not supported: %v", unsupported)
366366
}
367367
return nil
368368
}

0 commit comments

Comments
 (0)