Skip to content

Commit

Permalink
[chore][enhance] udpate error handler for api create new server
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 20, 2024
1 parent 10daeef commit dbebe24
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 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 @@ -60,4 +60,5 @@ const (
const (
EcVServerServerNotFound = ErrorCode("VngCloudVServerServerNotFound")
EcVServerServerDeleteCreatingServer = ErrorCode("VngCloudVServerServerDeleteCreatingServer")
EcVServerServerExceedQuota = ErrorCode("VngCloudVServerServerExceedQuota")
)
20 changes: 18 additions & 2 deletions vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package sdk_error
import lstr "strings"

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"
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"
)

func WithErrorServerNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
Expand Down Expand Up @@ -36,3 +37,18 @@ func WithErrorServerDeleteCreatingServer(perrResp IErrorRespone) func(sdkError I
}
}
}

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerExceedQuota) {
sdkError.WithErrorCode(EcVServerServerExceedQuota).
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 @@ -20,6 +20,7 @@ func (s *ComputeServiceV2) CreateServer(popts ICreateServerRequest) (*lsentity.S
return nil, lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorOutOfPoc(errResp),
lserr.WithErrorSubnetNotFound(errResp),
lserr.WithErrorServerExceedQuota(errResp),
lserr.WithErrorVolumeTypeNotFound(errResp),
lserr.WithErrorNetworkNotFound(errResp)).
WithKVparameters("projectId", s.getProjectId())
Expand Down

0 comments on commit dbebe24

Please sign in to comment.