From 512e43045068a10a63f82ed369741e6d31991914 Mon Sep 17 00:00:00 2001 From: Michael Sverdlov Date: Sun, 18 Aug 2024 11:42:21 +0300 Subject: [PATCH] Improve token and build info service errors (#1005) --- artifactory/services/buildinfo.go | 7 +++--- artifactory/services/security.go | 42 ++++++++++++++++++------------- artifactory/services/system.go | 2 +- tests/artifactoryctx_test.go | 7 ++---- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/artifactory/services/buildinfo.go b/artifactory/services/buildinfo.go index 4a1f2f317..bd50040ed 100644 --- a/artifactory/services/buildinfo.go +++ b/artifactory/services/buildinfo.go @@ -2,6 +2,7 @@ package services import ( "encoding/json" + "fmt" buildinfo "github.com/jfrog/build-info-go/entities" "net/http" @@ -61,14 +62,14 @@ func (bis *BuildInfoService) PublishBuildInfo(build *buildinfo.BuildInfo, projec if bis.IsDryRun() { log.Info("[Dry run] Logging Build info preview...") log.Output(clientutils.IndentJson(content)) - return summary, err + return summary, nil } httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails() utils.SetContentType("application/vnd.org.jfrog.artifactory+json", &httpClientsDetails.Headers) - log.Info("Deploying build info...") + log.Info(fmt.Sprintf("Publishing build info for <%s>/<%s>...", build.Name, build.Number)) resp, body, err := bis.client.SendPut(bis.GetArtifactoryDetails().GetUrl()+"api/build"+utils.GetProjectQueryParam(projectKey), content, &httpClientsDetails) if err != nil { - return summary, err + return summary, fmt.Errorf("error occurred while publishing build info: %s", err.Error()) } if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated, http.StatusNoContent); err != nil { return summary, err diff --git a/artifactory/services/security.go b/artifactory/services/security.go index 78153ec33..2f05ecc65 100644 --- a/artifactory/services/security.go +++ b/artifactory/services/security.go @@ -3,6 +3,7 @@ package services import ( "encoding/json" "errors" + "fmt" "net/http" "net/url" "strconv" @@ -14,8 +15,13 @@ import ( "github.com/jfrog/jfrog-client-go/utils/errorutils" ) -const tokenPath = "api/security/token" -const APIKeyPath = "api/security/apiKey" +const ( + tokenPath = "api/security/token" + APIKeyPath = "api/security/apiKey" + errorMsgPrefix = "error occurred while attempting to" + unexpectedServerResponsePrefix = "got unexpected server response while attempting to" + couldntParseServerResponsePrefix = "couldn't parse server response while attempting to" +) type SecurityService struct { client *jfroghttpclient.JfrogHttpClient @@ -94,7 +100,7 @@ func (ss *SecurityService) GetAPIKey() (string, error) { func getApiKeyFromBody(body []byte) (string, error) { var data = make(map[string]interface{}) if err := json.Unmarshal(body, &data); err != nil { - return "", errorutils.CheckErrorf("unable to decode json. Error: %w Upstream response: %s", err, string(body)) + return "", errorutils.CheckErrorf("unable to decode json. Error: %s Upstream response: %s", err.Error(), string(body)) } if len(data) == 0 { @@ -114,15 +120,15 @@ func (ss *SecurityService) CreateToken(params CreateTokenParams) (auth.CreateTok resp, body, err := ss.client.SendPostForm(artifactoryUrl+tokenPath, data, &httpClientsDetails) tokenInfo := auth.CreateTokenResponseData{} if err != nil { - return tokenInfo, err + return tokenInfo, fmt.Errorf("%s create token: %w", errorMsgPrefix, err) } if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { - return tokenInfo, err + return tokenInfo, fmt.Errorf("%s create token: %w", unexpectedServerResponsePrefix, err) } if err = json.Unmarshal(body, &tokenInfo); err != nil { - return tokenInfo, errorutils.CheckError(err) + return tokenInfo, errorutils.CheckErrorf("%s create token: %s", couldntParseServerResponsePrefix, err.Error()) } - return tokenInfo, err + return tokenInfo, nil } func (ss *SecurityService) GetTokens() (GetTokensResponseData, error) { @@ -131,15 +137,15 @@ func (ss *SecurityService) GetTokens() (GetTokensResponseData, error) { resp, body, _, err := ss.client.SendGet(artifactoryUrl+tokenPath, true, &httpClientsDetails) tokens := GetTokensResponseData{} if err != nil { - return tokens, err + return tokens, fmt.Errorf("%s get tokens: %w", errorMsgPrefix, err) } if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { - return tokens, err + return tokens, fmt.Errorf("%s get tokens: %w", unexpectedServerResponsePrefix, err) } if err = json.Unmarshal(body, &tokens); err != nil { - return tokens, errorutils.CheckError(err) + return tokens, errorutils.CheckErrorf("%s get tokens: %s", couldntParseServerResponsePrefix, err.Error()) } - return tokens, err + return tokens, nil } func (ss *SecurityService) GetUserTokens(username string) ([]string, error) { @@ -163,15 +169,15 @@ func (ss *SecurityService) RefreshToken(params ArtifactoryRefreshTokenParams) (a resp, body, err := ss.client.SendPostForm(artifactoryUrl+tokenPath, data, &httpClientsDetails) tokenInfo := auth.CreateTokenResponseData{} if err != nil { - return tokenInfo, err + return tokenInfo, fmt.Errorf("%s refresh token: %w", errorMsgPrefix, err) } if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { - return tokenInfo, err + return tokenInfo, fmt.Errorf("%s refresh token: %w", unexpectedServerResponsePrefix, err) } if err = json.Unmarshal(body, &tokenInfo); err != nil { - return tokenInfo, errorutils.CheckError(err) + return tokenInfo, errorutils.CheckErrorf("%s refresh token: %s", couldntParseServerResponsePrefix, err.Error()) } - return tokenInfo, err + return tokenInfo, nil } func (ss *SecurityService) RevokeToken(params RevokeTokenParams) (string, error) { @@ -181,12 +187,12 @@ func (ss *SecurityService) RevokeToken(params RevokeTokenParams) (string, error) data := buildRevokeTokenUrlValues(params) resp, body, err := ss.client.SendPostForm(requestFullUrl, data, &httpClientsDetails) if err != nil { - return "", err + return "", fmt.Errorf("%s revoke token: %w", errorMsgPrefix, err) } if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil { - return "", err + return "", fmt.Errorf("%s revoke token: %w", unexpectedServerResponsePrefix, err) } - return string(body), err + return string(body), nil } func buildCreateTokenUrlValues(params CreateTokenParams) url.Values { diff --git a/artifactory/services/system.go b/artifactory/services/system.go index 5e10c068d..f2f5f1070 100644 --- a/artifactory/services/system.go +++ b/artifactory/services/system.go @@ -119,7 +119,7 @@ func (ss *SystemService) sendGet(endpoint string) ([]byte, error) { httpDetails := (*ss.artDetails).CreateHttpClientDetails() resp, body, _, err := ss.client.SendGet((*ss.artDetails).GetUrl()+apiSystem+endpoint, true, &httpDetails) if err != nil { - return nil, err + return nil, fmt.Errorf("error occurred while attempting to get JFrog Artifactory %s: %s", endpoint, err.Error()) } if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK, http.StatusCreated); err != nil { return nil, fmt.Errorf("got unexpected server response while attempting to get JFrog Artifactory %s:\n%s", endpoint, err.Error()) diff --git a/tests/artifactoryctx_test.go b/tests/artifactoryctx_test.go index bba93dc9d..c042e0ce3 100644 --- a/tests/artifactoryctx_test.go +++ b/tests/artifactoryctx_test.go @@ -2,7 +2,6 @@ package tests import ( "context" - "errors" "testing" "time" @@ -40,8 +39,6 @@ func testCtxTimeout(t *testing.T) { assert.NoError(t, err) time.Sleep(time.Millisecond * 300) _, err = sm.GetVersion() - assert.Error(t, err) - if !errors.Is(err, context.DeadlineExceeded) { - t.Fail() - } + // Expect timeout error + assert.ErrorContains(t, err, context.DeadlineExceeded.Error()) }