Skip to content

Commit

Permalink
[feat] enhance error handler for api delete virtual address
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Nov 21, 2024
1 parent 94e93b6 commit deae827
Show file tree
Hide file tree
Showing 3 changed files with 21 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 @@ -112,6 +112,7 @@ const (
const (
EcVServerVirtualAddressNotFound = ErrorCode("VngCloudVServerVirtualAddressNotFound")
EcVServerVirtualAddressExceedQuota = ErrorCode("VngCloudVServerVirtualAddressExceedQuota")
EcVServerVirtualAddressInUse = ErrorCode("VngCloudVServerVirtualAddressInUse")
)

// vServer quota
Expand Down
19 changes: 18 additions & 1 deletion vngcloud/sdk_error/virtualaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
patternVirtualAddressNotFound = `virtual ip address with id [^.]+ is not found`
patternAddressPairNotFound = `address pair with uuid: [^.]+ was not existing`
patternVirtualAddressExceedQuota = "exceeded virtual_ip_address quota"
patternVirtualAddressInUse = "ip address is already in use"
)

var (
Expand Down Expand Up @@ -60,4 +61,20 @@ func WithErrorVirtualAddressExceedQuota(perrResp IErrorRespone) func(sdkError IE
WithErrorCategories(ErrCatQuota)
}
}
}
}

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternVirtualAddressInUse) {
sdkError.WithErrorCode(EcVServerVirtualAddressInUse).
WithMessage(errMsg).
WithErrors(perrResp.GetError()).
WithErrorCategories(ErrCatQuota)
}
}
}
3 changes: 2 additions & 1 deletion vngcloud/services/network/v2/virtualaddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func (s *NetworkServiceV2) DeleteVirtualAddressById(popts IDeleteVirtualAddressB

if _, sdkErr := s.VserverClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorVirtualAddressNotFound(errResp)).
lserr.WithErrorVirtualAddressNotFound(errResp),
lserr.WithErrorVirtualAddressInUse(errResp)).
WithKVparameters(popts.ToMap()).
WithErrorCategories(lserr.ErrCatVServer, lserr.ErrCatVirtualAddress)
}
Expand Down

0 comments on commit deae827

Please sign in to comment.