From fa03cb8a3fdba6984a206b55e2d9a9edcedb18a2 Mon Sep 17 00:00:00 2001 From: den-rgb Date: Fri, 24 Nov 2023 14:40:17 +0000 Subject: [PATCH] OCM-3963 | feat: Moving error helpers to ocm-common --- pkg/aws/errors/errors.go | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 pkg/aws/errors/errors.go diff --git a/pkg/aws/errors/errors.go b/pkg/aws/errors/errors.go new file mode 100644 index 0000000..0f00b0a --- /dev/null +++ b/pkg/aws/errors/errors.go @@ -0,0 +1,70 @@ +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" +) + +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) +} +