From 4bc8cdb6155e550ac8a74d0b55437dbf2e6dba00 Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Thu, 29 Aug 2024 10:57:00 +0700 Subject: [PATCH] [chore] update for http client --- vngcloud/client/http.go | 2 +- vngcloud/sdk_error/common.go | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/vngcloud/client/http.go b/vngcloud/client/http.go index 2febc79..4bb2262 100644 --- a/vngcloud/client/http.go +++ b/vngcloud/client/http.go @@ -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 { diff --git a/vngcloud/sdk_error/common.go b/vngcloud/sdk_error/common.go index e3de7b8..1b21093 100644 --- a/vngcloud/sdk_error/common.go +++ b/vngcloud/sdk_error/common.go @@ -3,6 +3,8 @@ package sdk_error import ( lfmt "fmt" lstr "strings" + + lreq "github.com/imroc/req/v3" ) const ( @@ -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, + }) } }