Skip to content

Commit 1ac4194

Browse files
committed
aws: Use errors.AsType in EC2 API error helpers
Replace `errors.As` usage in `pkg/aws/api` with the generic `errors.AsType` introduced in Go 1.26, dropping the intermediate target variables. See https://go.dev/gopls/release/v0.21.0#errorsastype-analyzer Signed-off-by: Hadrien Patte <hadrien.patte@datadoghq.com>
1 parent cfbd27e commit 1ac4194

2 files changed

Lines changed: 5 additions & 10 deletions

File tree

pkg/aws/api/api.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ func (c *Client) getMaxResults() *int32 {
129129
// isOperationNotPermitted checks if an error is an AWS OperationNotPermitted error,
130130
// which indicates the request returned too many results without sufficient filtering or pagination.
131131
func isOperationNotPermitted(err error) bool {
132-
var apiErr smithy.APIError
133-
if errors.As(err, &apiErr) {
132+
if apiErr, ok := errors.AsType[smithy.APIError](err); ok {
134133
return apiErr.ErrorCode() == OperationNotPermittedStr
135134
}
136135
return false
@@ -140,8 +139,7 @@ func isOperationNotPermitted(err error) bool {
140139
// IAM permission for the requested action. EC2 reports this as an
141140
// UnauthorizedOperation API error with HTTP status 403.
142141
func isUnauthorizedOperation(err error) bool {
143-
var apiErr smithy.APIError
144-
if errors.As(err, &apiErr) {
142+
if apiErr, ok := errors.AsType[smithy.APIError](err); ok {
145143
return apiErr.ErrorCode() == UnauthorizedOperationStr
146144
}
147145
return false
@@ -244,8 +242,7 @@ func MergeTags(tagMaps ...map[string]string) map[string]string {
244242
// the AWS API server. If no specific status is provided, either "OK" or
245243
// "Failed" is returned based on the error variable.
246244
func deriveStatus(err error) string {
247-
var respErr *awshttp.ResponseError
248-
if errors.As(err, &respErr) {
245+
if respErr, ok := errors.AsType[*awshttp.ResponseError](err); ok {
249246
return respErr.Response.Status
250247
}
251248

pkg/aws/ipam/node.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,7 @@ func (n *Node) PrepareIPAllocation(scopedLog *slog.Logger) (a *nodemanager.Alloc
529529

530530
// isSubnetAtPrefixCapacity parses error from AWS SDK to understand if the subnet is out of capacity for /28 prefixes.
531531
func isSubnetAtPrefixCapacity(err error) bool {
532-
var apiErr smithy.APIError
533-
if errors.As(err, &apiErr) {
532+
if apiErr, ok := errors.AsType[smithy.APIError](err); ok {
534533
return apiErr.ErrorCode() == api.InsufficientPrefixesInSubnetStr ||
535534
(apiErr.ErrorCode() == api.InvalidParameterValueStr &&
536535
strings.Contains(apiErr.ErrorMessage(), api.SubnetFullErrMsgStr))
@@ -652,8 +651,7 @@ func (n *Node) errorInstanceNotRunning(err error) (notRunning bool) {
652651
}
653652

654653
func isAttachmentIndexConflict(err error) bool {
655-
var apiErr smithy.APIError
656-
if errors.As(err, &apiErr) {
654+
if apiErr, ok := errors.AsType[smithy.APIError](err); ok {
657655
return apiErr.ErrorCode() == api.InvalidParameterValueStr &&
658656
strings.Contains(apiErr.ErrorMessage(), "interface attached at device")
659657
}

0 commit comments

Comments
 (0)