Skip to content

Commit

Permalink
[chore] update for http client
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Aug 29, 2024
1 parent ef112a7 commit 4bc8cdb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion vngcloud/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lser
return resp, lserr.ErrorHandler(resp.Err)
}

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

func (s *httpClient) needReauth(preq IRequest) bool {
Expand Down
22 changes: 20 additions & 2 deletions vngcloud/sdk_error/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sdk_error
import (
lfmt "fmt"
lstr "strings"

lreq "github.com/imroc/req/v3"
)

const (
Expand Down Expand Up @@ -108,10 +110,26 @@ func WithErrorPagingInvalid(perrResp IErrorRespone) func(sdkError IError) {
}
}

func WithErrorUnexpected() func(IError) {
func WithErrorUnexpected(presponse *lreq.Response) func(IError) {
statusCode := 0
url := ""
if presponse != nil {
if presponse.StatusCode != 0 {
statusCode = presponse.StatusCode
}

if presponse.Request != nil && presponse.Request.URL != nil {
url = presponse.Request.URL.String()
}
}

return func(sdkErr IError) {
sdkErr.WithErrorCode(EcUnexpectedError).
WithMessage("Unexpected Error").
WithErrors(lfmt.Errorf("unexpected error from making request to external service"))
WithErrors(lfmt.Errorf("unexpected error from making request to external service")).
WithParameters(map[string]interface{}{
"statusCode": statusCode,
"url": url,
})
}
}

0 comments on commit 4bc8cdb

Please sign in to comment.