Skip to content

Commit

Permalink
[chore] enhance error handler for api delete lb
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Oct 12, 2024
1 parent 3735290 commit db53c52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const (
EcVLBMemberMustIdentical = ErrorCode("VngCloudVLBMemberMustIdentical")
EcVLBLoadBalancerExceedQuota = ErrorCode("VngCloudVLBLoadBalancerExceedQuota")
EcVLBLoadBalancerIsDeleting = ErrorCode("VngCloudVLBLoadBalancerIsDeleting")
EcVLBLoadBalancerIsCreating = ErrorCode("VngCloudVLBLoadBalancerIsCreating")
)

// Endpoint
Expand Down
19 changes: 18 additions & 1 deletion vngcloud/sdk_error/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ const (
patternPoolIsUpdating = `pool id [^.]+ is updating`
patternLoadBalancerExceedQuota = "exceeded load_balancer quota. current used"
patternLoadBalancerIsDeleting = `load balancer id [^.]+ is deleting`
patternLoadBalancerIsCreating = `load balancer id [^.]+ is creating`
)

var (
regexErrorLoadBalancerNotReady = lregexp.MustCompile(patternLoadBalancerNotReady)
regexErrorListenerNotReady = lregexp.MustCompile(patternListenerNotReady)
regexErrorPoolIsUpdating = lregexp.MustCompile(patternPoolIsUpdating)
regexErrorLoadBalancerIsDeleting = lregexp.MustCompile(patternLoadBalancerIsDeleting)
regexErrorLoadBalancerIsCreating = lregexp.MustCompile(patternLoadBalancerIsCreating)
)

func WithErrorLoadBalancerNotFound(perrResp IErrorRespone) func(sdkError IError) {
Expand Down Expand Up @@ -177,14 +179,29 @@ func WithErrorLoadBalancerIsDeleting(perrResp IErrorRespone) func(sdkError IErro
}

errMsg := lstr.ToLower(lstr.TrimSpace(perrResp.GetMessage()))
if regexErrorLoadBalancerIsDeleting.FindString(errMsg) != "" {
if regexErrorLoadBalancerIsDeleting.FindString(errMsg) != "" {
sdkError.WithErrorCode(EcVLBLoadBalancerIsDeleting).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}

func WithErrorLoadBalancerIsCreating(perrResp IErrorRespone) func(sdkError IError) {
return func(sdkError IError) {
if perrResp == nil {
return
}

errMsg := lstr.ToLower(lstr.TrimSpace(perrResp.GetMessage()))
if regexErrorLoadBalancerIsCreating.FindString(errMsg) != "" {
sdkError.WithErrorCode(EcVLBLoadBalancerIsCreating).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}

func WithErrorListenerNotFound(perrResp IErrorRespone) func(sdkError IError) {
return func(sdkError IError) {
if perrResp == nil {
Expand Down
1 change: 1 addition & 0 deletions vngcloud/services/loadbalancer/v2/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func (s *LoadBalancerServiceV2) DeleteLoadBalancerById(popts IDeleteLoadBalancer
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorLoadBalancerNotFound(errResp),
lserr.WithErrorLoadBalancerNotReady(errResp),
lserr.WithErrorLoadBalancerIsCreating(errResp),
lserr.WithErrorLoadBalancerIsDeleting(errResp)).
WithKVparameters(
"loadBalancerId", popts.GetLoadBalancerId(),
Expand Down

0 comments on commit db53c52

Please sign in to comment.