Skip to content

Commit

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

// Endpoint
Expand Down
23 changes: 20 additions & 3 deletions vngcloud/sdk_error/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ const (
patternMemberMustIdentical = "the members provided are identical to the existing members in the pool"
patternPoolIsUpdating = `pool id [^.]+ is updating`
patternLoadBalancerExceedQuota = "exceeded load_balancer quota. current used"
patternLoadBalancerIsDeleting = `load balancer id [^.]+ is deleting`
)

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

func WithErrorLoadBalancerNotFound(perrResp IErrorRespone) func(sdkError IError) {
Expand Down Expand Up @@ -168,6 +170,21 @@ func WithErrorLoadBalancerNotReady(perrResp IErrorRespone) func(sdkError IError)
}
}

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

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

func WithErrorListenerNotFound(perrResp IErrorRespone) func(sdkError IError) {
return func(sdkError IError) {
if perrResp == nil {
Expand Down
3 changes: 2 additions & 1 deletion vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func (s *ComputeServiceV2) DeleteServerById(popts IDeleteServerByIdRequest) lser
lserr.WithErrorServerNotFound(errResp),
lserr.WithErrorServerDeleteDeletingServer(errResp),
lserr.WithErrorServerDeleteBillingServer(errResp),
lserr.WithErrorServerDeleteCreatingServer(errResp)).
lserr.WithErrorServerDeleteCreatingServer(errResp),
lserr.WithErrorVolumeInProcess(errResp)).
WithKVparameters("projectId", s.getProjectId(),
"serverId", popts.GetServerId())
}
Expand Down
6 changes: 5 additions & 1 deletion vngcloud/services/loadbalancer/v2/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ func (s *LoadBalancerServiceV2) DeleteLoadBalancerById(popts IDeleteLoadBalancer
if _, sdkErr := s.VLBClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorLoadBalancerNotFound(errResp),
lserr.WithErrorLoadBalancerNotReady(errResp))
lserr.WithErrorLoadBalancerNotReady(errResp),
lserr.WithErrorLoadBalancerIsDeleting(errResp)).
WithKVparameters(
"loadBalancerId", popts.GetLoadBalancerId(),
"projectId", s.getProjectId())
}

return nil
Expand Down

0 comments on commit 7898cef

Please sign in to comment.