Skip to content

Commit

Permalink
[chore] enhance error categories mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Sep 9, 2024
1 parent 7950e32 commit d9f51b2
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
5 changes: 5 additions & 0 deletions vngcloud/sdk_error/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ const (
const (
ErrCatIam ErrorCategory = "iam"
)

// infra error category
const (
ErrCatInfra ErrorCategory = "infra"
)
9 changes: 9 additions & 0 deletions vngcloud/sdk_error/isdk_error.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion vngcloud/sdk_error/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions vngcloud/sdk_error/sdk_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion vngcloud/sdk_error/secgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion vngcloud/sdk_error/secgroup_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
9 changes: 6 additions & 3 deletions vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions vngcloud/sdk_error/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand Down

0 comments on commit d9f51b2

Please sign in to comment.