From 3378d21dbf5f79d4da36b0bf7ba5891cd01f23e1 Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Mon, 13 May 2024 08:16:04 +0700 Subject: [PATCH] [enhance] add error handler for the secgroup apis --- vngcloud/errors/secgroup.go | 80 ++++++++++++++++++ .../services/network/v2/secgroup/errors.go | 41 --------- .../services/network/v2/secgroup/secgroup.go | 84 ++++++------------- 3 files changed, 105 insertions(+), 100 deletions(-) create mode 100644 vngcloud/errors/secgroup.go delete mode 100644 vngcloud/services/network/v2/secgroup/errors.go diff --git a/vngcloud/errors/secgroup.go b/vngcloud/errors/secgroup.go new file mode 100644 index 0000000..d348a8a --- /dev/null +++ b/vngcloud/errors/secgroup.go @@ -0,0 +1,80 @@ +package errors + +import ( + lssdkErr "github.com/vngcloud/vngcloud-go-sdk/error" + lstr "strings" +) + +const ( + patternSecgroupNameAlreadyExists = "name of security group already exist" + patternSecgroupExceedQuota = "exceeded secgroup quota" + patternSecgroupInUse = "securitygroupinuse" + patternSecgroupNotFound = "cannot get security group with id" +) + +var ( + ErrCodeSecgroupNameAlreadyExists lssdkErr.ErrorCode = "ErrorSecgroupNameAlreadyExists" + ErrCodeSecgroupExceedQuota lssdkErr.ErrorCode = "ErrorSecgroupExceedQuota" + ErrCodeSecgroupInUse lssdkErr.ErrorCode = "ErrorSecgroupInUse" + ErrCodeSecgroupNotFound lssdkErr.ErrorCode = "ErrorSecgroupNotFound" +) + +func WithErrorSecgroupNameAlreadyExists(perrResp *lssdkErr.ErrorResponse, perr error) func(*lssdkErr.SdkError) { + return func(sdkError *lssdkErr.SdkError) { + if perrResp == nil { + return + } + + errMsg := perrResp.Message + if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternSecgroupNameAlreadyExists) { + sdkError.Code = ErrCodeSecgroupNameAlreadyExists + sdkError.Message = errMsg + sdkError.Error = perr + } + } +} + +func WithErrorSecgroupExceedQuota(perrResp *lssdkErr.ErrorResponse, perr error) func(*lssdkErr.SdkError) { + return func(sdkError *lssdkErr.SdkError) { + if perrResp == nil { + return + } + + errMsg := perrResp.Message + if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternSecgroupExceedQuota) { + sdkError.Code = ErrCodeSecgroupExceedQuota + sdkError.Message = errMsg + sdkError.Error = perr + } + } +} + +func WithErrorSecgroupInUse(perrResp *lssdkErr.ErrorResponse, perr error) func(*lssdkErr.SdkError) { + return func(sdkError *lssdkErr.SdkError) { + if perrResp == nil { + return + } + + errMsg := perrResp.Message + if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternSecgroupInUse) { + sdkError.Code = ErrCodeSecgroupInUse + sdkError.Message = errMsg + sdkError.Error = perr + } + } +} + +func WithErrorSecgroupNotFound(perrResp *lssdkErr.ErrorResponse, perr error) func(*lssdkErr.SdkError) { + return func(sdkError *lssdkErr.SdkError) { + if perrResp == nil { + return + } + + errMsg := perrResp.Message + if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternSecgroupNotFound) { + sdkError.Code = ErrCodeSecgroupNotFound + sdkError.Message = errMsg + sdkError.Error = perr + } + } +} diff --git a/vngcloud/services/network/v2/secgroup/errors.go b/vngcloud/services/network/v2/secgroup/errors.go deleted file mode 100644 index b16867e..0000000 --- a/vngcloud/services/network/v2/secgroup/errors.go +++ /dev/null @@ -1,41 +0,0 @@ -package secgroup - -import ( - "fmt" - vconError "github.com/vngcloud/vngcloud-go-sdk/error" -) - -const ( - patternErrNotFound = "Cannot get security group with id" - patternSecgroupInUse = "SecurityGroupInUse" -) - -var ( - ErrSecgroupNotFound vconError.ErrorCode = "SECURITY_GROUP_NOT_FOUND" - ErrSecgroupUnknown vconError.ErrorCode = "SECURITY_GROUP_UNKNOWN_ERROR" - ErrSecgroupInUse vconError.ErrorCode = "SECURITY_GROUP_IN_USE" -) - -func NewErrNameDuplicate(pName, pInfo string) vconError.IErrorBuilder { - err := new(ErrNameDuplicate) - err.Name = pName - if pInfo != "" { - err.Info = pInfo - } - return err -} - -func IsErrNameDuplicate(pErr error) bool { - _, ok := pErr.(*ErrNameDuplicate) - return ok -} - -type ErrNameDuplicate struct { - Name string - vconError.BaseError -} - -func (s *ErrNameDuplicate) Error() string { - s.DefaultError = fmt.Sprintf("the secgroup [%s] is duplicated by name", s.Name) - return s.ChoseErrString() -} diff --git a/vngcloud/services/network/v2/secgroup/secgroup.go b/vngcloud/services/network/v2/secgroup/secgroup.go index 675bfe4..b793692 100644 --- a/vngcloud/services/network/v2/secgroup/secgroup.go +++ b/vngcloud/services/network/v2/secgroup/secgroup.go @@ -1,102 +1,68 @@ package secgroup import ( - "encoding/json" - "github.com/vngcloud/vngcloud-go-sdk/client" - lsdkError "github.com/vngcloud/vngcloud-go-sdk/error" - "github.com/vngcloud/vngcloud-go-sdk/vngcloud/objects" - "strings" + lsclient "github.com/vngcloud/vngcloud-go-sdk/client" + lserrors "github.com/vngcloud/vngcloud-go-sdk/error" + lserror "github.com/vngcloud/vngcloud-go-sdk/vngcloud/errors" + lsobj "github.com/vngcloud/vngcloud-go-sdk/vngcloud/objects" ) -func Create(pSc *client.ServiceClient, pOpts ICreateOptsBuilder) (*objects.Secgroup, error) { +func Create(psc *lsclient.ServiceClient, popts ICreateOptsBuilder) (*lsobj.Secgroup, *lserrors.SdkError) { response := NewCreateResponse() - body := pOpts.ToRequestBody() - reqRes, err := pSc.Post(createURL(pSc, pOpts), &client.RequestOpts{ + body := popts.ToRequestBody() + errResp := lserrors.NewErrorResponse() + _, err := psc.Post(createURL(psc, popts), &lsclient.RequestOpts{ JSONBody: body, JSONResponse: response, + JSONError: errResp, OkCodes: []int{201}, }) if err != nil { - result := make(map[string]interface{}) - err2 := json.Unmarshal(reqRes.Bytes(), &result) - if err2 == nil { - if message, _ := result["message"].(string); strings.Contains(strings.ToLower(strings.TrimSpace(message)), "name of security group already exist") { - return nil, NewErrNameDuplicate("", "name is already used") - } - } - - return nil, err + return nil, lserror.ErrorHandler(err, + lserror.WithErrorSecgroupNameAlreadyExists(errResp, err), + lserror.WithErrorSecgroupExceedQuota(errResp, err)) } return response.ToSecgroupObject(), nil } -func Delete(pSc *client.ServiceClient, pOpts IDeleteOptsBuilder) *lsdkError.SdkError { - errResp := lsdkError.NewErrorResponse() - _, err := pSc.Delete(deleteURL(pSc, pOpts), &client.RequestOpts{ +func Delete(pSc *lsclient.ServiceClient, pOpts IDeleteOptsBuilder) *lserrors.SdkError { + errResp := lserrors.NewErrorResponse() + _, err := pSc.Delete(deleteURL(pSc, pOpts), &lsclient.RequestOpts{ OkCodes: []int{204}, JSONError: errResp, }) if err != nil { - if strings.Contains(errResp.Message, patternSecgroupInUse) { - return &lsdkError.SdkError{ - Code: ErrSecgroupInUse, - Message: errResp.Message, - Error: err, - } - } - - if strings.Contains(errResp.Message, patternErrNotFound) { - return &lsdkError.SdkError{ - Code: ErrSecgroupNotFound, - Message: errResp.Message, - Error: err, - } - } - - return &lsdkError.SdkError{ - Code: ErrSecgroupUnknown, - Message: errResp.Message, - Error: err, - } + return lserror.ErrorHandler(err, + lserror.WithErrorSecgroupInUse(errResp, err), + lserror.WithErrorSecgroupNotFound(errResp, err)) } return nil } -func Get(pSc *client.ServiceClient, pOpts IGetOptsBuilder) (*objects.Secgroup, *lsdkError.SdkError) { +func Get(pSc *lsclient.ServiceClient, pOpts IGetOptsBuilder) (*lsobj.Secgroup, *lserrors.SdkError) { response := NewGetResponse() - errResp := lsdkError.NewErrorResponse() - _, err := pSc.Get(getURL(pSc, pOpts), &client.RequestOpts{ + errResp := lserrors.NewErrorResponse() + _, err := pSc.Get(getURL(pSc, pOpts), &lsclient.RequestOpts{ JSONResponse: response, JSONError: errResp, OkCodes: []int{200}, }) if err != nil { - if strings.Contains(errResp.Message, patternErrNotFound) { - return nil, &lsdkError.SdkError{ - Code: ErrSecgroupNotFound, - Message: errResp.Message, - Error: err, - } - } - - return nil, &lsdkError.SdkError{ - Code: ErrSecgroupUnknown, - Message: errResp.Message, - Error: err, - } + return nil, lserror.ErrorHandler(err, + lserror.WithErrorSecgroupNotFound(errResp, err)) } return response.ToSecgroupObject(), nil } -func List(pSc *client.ServiceClient, pOpts IListOptsBuilder) ([]*objects.Secgroup, error) { +func List(pSc *lsclient.ServiceClient, pOpts IListOptsBuilder) ([]*lsobj.Secgroup, error) { response := NewListResponse() - _, err := pSc.Get(listURL(pSc, pOpts), &client.RequestOpts{ + _, err := pSc.Get(listURL(pSc, pOpts), &lsclient.RequestOpts{ JSONResponse: response, OkCodes: []int{200}, })