-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OCM-3963 | feat: Moving error helpers to ocm-common
- Loading branch information
den-rgb
committed
Nov 24, 2023
1 parent
9c5e842
commit 9043b9d
Showing
3 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package aws | ||
|
||
import ( | ||
"errors" | ||
|
||
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types" | ||
"github.com/aws/smithy-go" | ||
) | ||
|
||
const ( | ||
InvalidClientTokenID = "InvalidClientTokenId" | ||
AccessDenied = "AccessDenied" | ||
Forbidden = "Forbidden" | ||
DryRunOperation = "DryRunOperation" | ||
UnauthorizedOperation = "UnauthorizedOperation" | ||
AuthFailure = "AuthFailure" | ||
OptInRequired = "OptInRequired" | ||
VpcLimitExceeded = "VpcLimitExceeded" | ||
LimitExceeded = "LimitExceeded" | ||
UnrecognizedClientException = "UnrecognizedClientException" | ||
IncompleteSignature = "IncompleteSignature" | ||
AccessDeniedException = "AccessDeniedException" | ||
NoSuchResourceException = "NoSuchResourceException" | ||
Throttling = "Throttling" | ||
SubnetNotFound = "InvalidSubnetID.NotFound" | ||
VolumeTypeNotAvailableInZone = "VolumeTypeNotAvailableInZone" | ||
InvalidParameterValue = "InvalidParameterValue" | ||
NoSuchHostedZone = "NoSuchHostedZone" | ||
DependencyViolation = "DependencyViolation" | ||
NoSuchEntity = "NoSuchEntity" | ||
InvalidRouteTableID = "InvalidRouteTableID.NotFound" | ||
InvalidInternetGatewayID = "InvalidInternetGatewayID.NotFound" | ||
InvalidVpcID = "InvalidVpcID.NotFound" | ||
InvalidAllocationID = "InvalidAllocationID.NotFound" | ||
InvalidGroup = "InvalidGroup.NotFound" | ||
InvalidSubnetID = "InvalidSubnetId.NotFound" | ||
) | ||
|
||
func IsErrorCode(err error, code string) bool { | ||
var apiErr smithy.APIError | ||
return errors.As(err, &apiErr) && apiErr.ErrorCode() == code | ||
} | ||
|
||
func IsSubnetNotFoundError(err error) bool { | ||
return IsErrorCode(err, SubnetNotFound) | ||
} | ||
|
||
func IsThrottle(err error) bool { | ||
return IsErrorCode(err, Throttling) | ||
} | ||
|
||
func IsEntityAlreadyExistsException(err error) bool { | ||
var entityAlreadyExists *iamtypes.EntityAlreadyExistsException | ||
return errors.As(err, &entityAlreadyExists) | ||
} | ||
|
||
func IsNoSuchEntityException(err error) bool { | ||
var noSuchEntity *iamtypes.NoSuchEntityException | ||
return errors.As(err, &noSuchEntity) | ||
} | ||
|
||
func IsAccessDeniedException(err error) bool { | ||
return IsErrorCode(err, AccessDenied) | ||
} | ||
|
||
func IsForbiddenException(err error) bool { | ||
return IsErrorCode(err, Forbidden) | ||
} | ||
|
||
func IsLimitExceededException(err error) bool { | ||
return IsErrorCode(err, LimitExceeded) | ||
} | ||
|
||
func IsInvalidTokenException(err error) bool { | ||
return IsErrorCode(err, InvalidClientTokenID) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package aws | ||
|
||
import ( | ||
"errors" | ||
|
||
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types" | ||
smithy "github.com/aws/smithy-go" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
) | ||
|
||
var _ = Describe("Error Checks", func() { | ||
It("should identify NoSuchEntityException", func() { | ||
err := &iamtypes.NoSuchEntityException{} | ||
Expect(IsNoSuchEntityException(err)).To(BeTrue()) | ||
Expect(IsNoSuchEntityException(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify EntityAlreadyExistsException", func() { | ||
err := &iamtypes.EntityAlreadyExistsException{} | ||
Expect(IsEntityAlreadyExistsException(err)).To(BeTrue()) | ||
Expect(IsEntityAlreadyExistsException(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify IsThrottle", func() { | ||
err := &smithy.GenericAPIError{Code: "Throttling"} | ||
Expect(IsThrottle(err)).To(BeTrue()) | ||
Expect(IsThrottle(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify IsAccessDeniedException", func() { | ||
err := &smithy.GenericAPIError{Code: "AccessDenied"} | ||
Expect(IsAccessDeniedException(err)).To(BeTrue()) | ||
Expect(IsAccessDeniedException(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify IsForbiddenException", func() { | ||
err := &smithy.GenericAPIError{Code: "Forbidden"} | ||
Expect(IsForbiddenException(err)).To(BeTrue()) | ||
Expect(IsForbiddenException(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify IsLimitExceededException", func() { | ||
err := &smithy.GenericAPIError{Code: "LimitExceeded"} | ||
Expect(IsLimitExceededException(err)).To(BeTrue()) | ||
Expect(IsLimitExceededException(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify IsInvalidTokenException", func() { | ||
err := &smithy.GenericAPIError{Code: "InvalidClientTokenId"} | ||
Expect(IsInvalidTokenException(err)).To(BeTrue()) | ||
Expect(IsInvalidTokenException(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify IsSubnetNotFoundError", func() { | ||
err := &smithy.GenericAPIError{Code: "InvalidSubnetID.NotFound"} | ||
Expect(IsSubnetNotFoundError(err)).To(BeTrue()) | ||
Expect(IsSubnetNotFoundError(errors.New("random error"))).To(BeFalse()) | ||
}) | ||
|
||
It("should identify ErrorCode", func() { | ||
err := &smithy.GenericAPIError{Code: "AccessDenied"} | ||
Expect(IsErrorCode(err, "AccessDenied")).To(BeTrue()) | ||
Expect(IsErrorCode(err, "DifferentCode")).To(BeFalse()) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestValidations(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Validations Suite") | ||
} |