Skip to content

Commit

Permalink
[feat][enhance] enhance error handler for api get token
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 22, 2024
1 parent 3680fa6 commit 303c46f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
const (
EcAuthenticationFailed = ErrorCode("VngCloudIamAuthenticationFailed")
EcTooManyFailedLogins = ErrorCode("VngCloudIamTooManyFailedLogins")
EcUnknownAuthFailure = ErrorCode("VngCloudIamUnknownAuthFailure")
)

// VServer secgroup
Expand Down
18 changes: 18 additions & 0 deletions vngcloud/sdk_error/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,21 @@ func WithErrorTooManyFailedLogin(perrResp IErrorRespone) func(ISdkError) {
}
}
}

func WithErrorUnknownAuthFailure(perrResp IErrorRespone) func(ISdkError) {
return func(sdkErr ISdkError) {
if perrResp == nil {
return
}

if perrResp.GetError() == nil {
return
}

if sdkErr.GetErrorCode() == EcUnknownError {
sdkErr.WithErrorCode(EcUnknownAuthFailure).
WithMessage(perrResp.GetMessage()).
WithErrors(perrResp.GetError())
}
}
}
4 changes: 3 additions & 1 deletion vngcloud/services/identity/v2/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func (s *IdentityServiceV2) GetAccessToken(popts IGetAccessTokenRequest) (*lsent
if _, sdkErr := s.IamClient.Post(url, req); sdkErr != nil {
return nil, lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorTooManyFailedLogin(errResp),
lserr.WithErrorAuthenticationFailed(errResp)).WithKVparameters("clientId", popts.GetClientId())
lserr.WithErrorAuthenticationFailed(errResp),
lserr.WithErrorUnknownAuthFailure(errResp)). // Always put this handler at the end
WithKVparameters("clientId", popts.GetClientId())
}

return resp.ToEntityAccessToken(), nil
Expand Down

0 comments on commit 303c46f

Please sign in to comment.