Skip to content

Commit

Permalink
[build][enhance] error handler when make request via proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Jul 18, 2024
1 parent e3ecfc7 commit ec54b9f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
6 changes: 3 additions & 3 deletions test/lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestCreateInterLoadBalancerSuccess3(t *ltesting.T) {
vngcloud := validSuperSdkConfig().WithProjectId("pro-c8e87532-dc1a-421c-8c5e-4604d772829f")
opt := lsinter.NewCreateLoadBalancerRequest(
getValueOfEnv("VINHPORTAL_USER_ID"),
"cuongdm3-test-intervpc",
"cuongdm3-demo-intervpc-2",
"lbp-96b6b072-aadb-4b58-9d5f-c16ad69d36aa",
"sub-01842d14-3476-4af7-b252-5a2cdbd37b38",
"sub-403b36d2-39fc-47c4-b40b-8df0ecb71045",
Expand Down Expand Up @@ -424,8 +424,8 @@ func TestDeleteLoadBalancer(t *ltesting.T) {
}

func TestListTagsSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lslbv2.NewListTagsRequest("lb-8c544dcd-b0fe-4d60-a5b4-ac81b473b475")
vngcloud := validSuperSdkConfig2()
opt := lslbv2.NewListTagsRequest("lb-b1153f05-fd44-4861-8b66-d8b811597faf")
tags, sdkErr := vngcloud.VLBGateway().V2().LoadBalancerService().ListTags(opt)
if sdkErr != nil {
t.Fatalf("Expect nil but got %+v", sdkErr)
Expand Down
48 changes: 26 additions & 22 deletions vngcloud/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,34 +126,38 @@ func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lser
return resp, lserr.ErrorHandler(err)
}

switch resp.StatusCode {
case lhttp.StatusUnauthorized:
if !preq.SkipAuthentication() && s.reauthFunc != nil {
if sdkErr := s.reauthenticate(); sdkErr != nil {
return nil, sdkErr
if resp.Response != nil {
switch resp.Response.StatusCode {
case lhttp.StatusUnauthorized:
if !preq.SkipAuthentication() && s.reauthFunc != nil {
if sdkErr := s.reauthenticate(); sdkErr != nil {
return nil, sdkErr
}

return s.DoRequest(purl, preq)
} else {
return nil, defaultErrorResponse(resp.Err, purl, preq, resp)
}
case lhttp.StatusTooManyRequests:
return nil, lserr.ErrorHandler(resp.Err)
case lhttp.StatusInternalServerError:
return nil, lserr.SdkErrorHandler(
defaultErrorResponse(resp.Err, purl, preq, resp), nil,
lserr.WithErrorInternalServerError())
case lhttp.StatusForbidden:
return nil, lserr.SdkErrorHandler(
defaultErrorResponse(resp.Err, purl, preq, resp), nil,
lserr.WithErrorPermissionDenied())
}

return s.DoRequest(purl, preq)
} else {
return nil, defaultErrorResponse(resp.Err, purl, preq, resp)
if preq.ContainsOkCode(resp.StatusCode) {
return resp, nil
}
case lhttp.StatusTooManyRequests:
return nil, lserr.ErrorHandler(resp.Err)
case lhttp.StatusInternalServerError:
return nil, lserr.SdkErrorHandler(
defaultErrorResponse(resp.Err, purl, preq, resp), nil,
lserr.WithErrorInternalServerError())
case lhttp.StatusForbidden:
return nil, lserr.SdkErrorHandler(
defaultErrorResponse(resp.Err, purl, preq, resp), nil,
lserr.WithErrorPermissionDenied())
}

if preq.ContainsOkCode(resp.StatusCode) {
return resp, nil
return resp, lserr.ErrorHandler(resp.Err)
}

return resp, lserr.ErrorHandler(resp.Err)
return nil, lserr.ErrorHandler(nil, lserr.WithErrorUnexpected())
}

func (s *httpClient) needReauth(preq IRequest) bool {
Expand Down
8 changes: 8 additions & 0 deletions vngcloud/sdk_error/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,11 @@ func WithErrorPagingInvalid(perrResp IErrorRespone) func(sdkError ISdkError) {
}
}
}

func WithErrorUnexpected() func(ISdkError) {
return func(sdkErr ISdkError) {
sdkErr.WithErrorCode(EcUnexpectedError).
WithMessage("Unexpected Error").
WithErrors(lfmt.Errorf("unexpected error from making request to external service"))
}
}
1 change: 1 addition & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
EcInternalServerError = ErrorCode("VngCloudApiInternalServerError")
EcPagingInvalid = ErrorCode("VngCloudApiPagingInvalid")
EcPermissionDenied = ErrorCode("VngCloudApiPermissionDenied")
EcUnexpectedError = ErrorCode("VngCloudApiUnexpectedError")
)

// Internal SDK error
Expand Down

0 comments on commit ec54b9f

Please sign in to comment.