diff --git a/vngcloud/sdk_error/categories.go b/vngcloud/sdk_error/categories.go index 1211767..6810304 100644 --- a/vngcloud/sdk_error/categories.go +++ b/vngcloud/sdk_error/categories.go @@ -10,3 +10,8 @@ const ( const ( ErrCatIam ErrorCategory = "iam" ) + +// infra error category +const ( + ErrCatInfra ErrorCategory = "infra" +) diff --git a/vngcloud/sdk_error/isdk_error.go b/vngcloud/sdk_error/isdk_error.go index e0d3016..9299690 100644 --- a/vngcloud/sdk_error/isdk_error.go +++ b/vngcloud/sdk_error/isdk_error.go @@ -1,8 +1,12 @@ package sdk_error +import ljset "github.com/cuongpiger/joat/data-structure/set" + type IError interface { IsError(perrCode ErrorCode) bool IsErrorAny(perrCodes ...ErrorCode) bool + IsCategory(pcategory ErrorCategory) bool + IsCategories(pcategories ...ErrorCategory) bool WithErrorCode(perrCode ErrorCode) IError WithMessage(pmsg string) IError @@ -16,8 +20,13 @@ type IError interface { GetErrorCode() ErrorCode GetStringErrorCode() string GetParameters() map[string]interface{} + GetErrorCategories() ljset.Set[ErrorCategory] GetErrorMessages() string GetListParameters() []interface{} + + RemoveCategories(pcategories ...ErrorCategory) IError + + AppendCategories(pcategories ...ErrorCategory) IError } type IErrorRespone interface { diff --git a/vngcloud/sdk_error/loadbalancer.go b/vngcloud/sdk_error/loadbalancer.go index 48b95f2..b55b4e2 100644 --- a/vngcloud/sdk_error/loadbalancer.go +++ b/vngcloud/sdk_error/loadbalancer.go @@ -70,7 +70,8 @@ func WithErrorLoadBalancerExceedQuota(perrResp IErrorRespone) func(sdkError IErr if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternLoadBalancerExceedQuota) { sdkError.WithErrorCode(EcVLBLoadBalancerExceedQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } } diff --git a/vngcloud/sdk_error/sdk_error.go b/vngcloud/sdk_error/sdk_error.go index d49e344..191e674 100644 --- a/vngcloud/sdk_error/sdk_error.go +++ b/vngcloud/sdk_error/sdk_error.go @@ -39,6 +39,22 @@ func (s *SdkError) IsErrorAny(perrCodes ...ErrorCode) bool { return false } +func (s *SdkError) IsCategory(pcategory ErrorCategory) bool { + if s.categories == nil { + return false + } + + return s.categories.ContainsOne(pcategory) +} + +func (s *SdkError) IsCategories(pcategories ...ErrorCategory) bool { + if s.categories == nil { + return false + } + + return s.categories.ContainsAny(pcategories...) +} + func (s *SdkError) WithErrorCode(perrCode ErrorCode) IError { s.errorCode = perrCode return s @@ -131,6 +147,10 @@ func (s *SdkError) GetParameters() map[string]interface{} { return s.parameters } +func (s *SdkError) GetErrorCategories() ljset.Set[ErrorCategory] { + return s.categories +} + func (s *SdkError) GetErrorMessages() string { if s.error == nil { return s.message @@ -151,3 +171,22 @@ func (s *SdkError) GetListParameters() []interface{} { return result } + +func (s *SdkError) RemoveCategories(pcategories ...ErrorCategory) IError { + if s.categories == nil { + return s + } + + s.categories.RemoveAll(pcategories...) + return s +} + +func (s *SdkError) AppendCategories(pcategories ...ErrorCategory) IError { + if s.categories == nil { + s.categories = ljset.NewSet[ErrorCategory](pcategories...) + return s + } + + s.categories.Append(pcategories...) + return s +} diff --git a/vngcloud/sdk_error/secgroup.go b/vngcloud/sdk_error/secgroup.go index cfbb65a..8b2e3cc 100644 --- a/vngcloud/sdk_error/secgroup.go +++ b/vngcloud/sdk_error/secgroup.go @@ -49,7 +49,8 @@ func WithErrorSecgroupExceedQuota(perrResp IErrorRespone) func(sdkError IError) if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternSecgroupExceedQuota) { sdkError.WithErrorCode(EcVServerSecgroupExceedQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } } diff --git a/vngcloud/sdk_error/secgroup_rule.go b/vngcloud/sdk_error/secgroup_rule.go index 0da0f7f..4e796ca 100644 --- a/vngcloud/sdk_error/secgroup_rule.go +++ b/vngcloud/sdk_error/secgroup_rule.go @@ -48,7 +48,8 @@ func WithErrorSecgroupRuleExceedQuota(perrResp IErrorRespone) func(sdkError IErr if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternSecgroupRuleExceedQuota) { sdkError.WithErrorCode(EcVServerSecgroupRuleExceedQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } } diff --git a/vngcloud/sdk_error/server.go b/vngcloud/sdk_error/server.go index 7fc35fb..e33aa4d 100644 --- a/vngcloud/sdk_error/server.go +++ b/vngcloud/sdk_error/server.go @@ -43,7 +43,8 @@ func WithErrorServerFlavorSystemExceedQuota(perrResp IErrorRespone) func(sdkErro if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerFlavorSystemExceedQuota) { sdkError.WithErrorCode(EcVServerServerFlavorSystemExceedQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatInfra) } } } @@ -103,7 +104,8 @@ func WithErrorServerExceedCpuQuota(perrResp IErrorRespone) func(sdkError IError) if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerExceedCpuQuota) { sdkError.WithErrorCode(EcVServerServerExceedCpuQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } } @@ -194,7 +196,8 @@ func WithErrorServerAttachVolumeQuotaExceeded(perrResp IErrorRespone) func(sdkEr if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerAttachVolumeQuotaExceeded) { sdkError.WithErrorCode(EcVServerServerVolumeAttachQuotaExceeded). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } } diff --git a/vngcloud/sdk_error/volume.go b/vngcloud/sdk_error/volume.go index 128230e..e828f4e 100644 --- a/vngcloud/sdk_error/volume.go +++ b/vngcloud/sdk_error/volume.go @@ -75,7 +75,8 @@ func WithErrorVolumeSizeExceedGlobalQuota(perrResp IErrorRespone) func(sdkError if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternVolumeSizeExceedGlobalQuota) { sdkError.WithErrorCode(EcVServerVolumeSizeExceedGlobalQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } } @@ -90,7 +91,8 @@ func WithErrorVolumeExceedQuota(perrResp IErrorRespone) func(sdkError IError) { if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternVolumeExceedQuota) { sdkError.WithErrorCode(EcVServerVolumeExceedQuota). WithMessage(errMsg). - WithErrors(perrResp.GetError()) + WithErrors(perrResp.GetError()). + WithErrorCategories(ErrCatQuota) } } }