Skip to content

Commit

Permalink
[enhance] update error handler for delete server api
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 21, 2024
1 parent dbebe24 commit def7f8a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 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 @@ -61,4 +61,5 @@ const (
EcVServerServerNotFound = ErrorCode("VngCloudVServerServerNotFound")
EcVServerServerDeleteCreatingServer = ErrorCode("VngCloudVServerServerDeleteCreatingServer")
EcVServerServerExceedQuota = ErrorCode("VngCloudVServerServerExceedQuota")
EcVServerServerDeleteDeletingServer = ErrorCode("VngCloudVServerServerDeleteDeletingServer")
)
16 changes: 16 additions & 0 deletions vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
patternServerNotFound = "cannot get server with id" // "Cannot get volume type with id vtype-6790f903-38d2-454d-919e-5b49184b5927"
patternServerCreating = "cannot delete server with status creating" // "Server is creating"
patternServerExceedQuota = "exceeded vm quota" // "The number of servers exceeds the quota"
patternServerDeleting = "cannot delete server with status deleting" // "Server is deleting"
)

func WithErrorServerNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
Expand Down Expand Up @@ -52,3 +53,18 @@ func WithErrorServerExceedQuota(perrResp IErrorRespone) func(sdkError ISdkError)
}
}
}

func WithErrorServerDeleteDeletingServer(perrResp IErrorRespone) func(sdkError ISdkError) {
return func(sdkError ISdkError) {
if perrResp == nil {
return
}

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerDeleting) {
sdkError.WithErrorCode(EcVServerServerDeleteDeletingServer).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}
1 change: 1 addition & 0 deletions vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (s *ComputeServiceV2) DeleteServerById(popts IDeleteServerByIdRequest) lser
if _, sdkErr := s.VserverClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorServerNotFound(errResp),
lserr.WithErrorServerDeleteDeletingServer(errResp),
lserr.WithErrorServerDeleteCreatingServer(errResp)).
WithKVparameters("projectId", s.getProjectId(),
"serverId", popts.GetServerId())
Expand Down

0 comments on commit def7f8a

Please sign in to comment.