Skip to content

Commit

Permalink
Added a new function to create HubClientErr with HTTP status code (bl…
Browse files Browse the repository at this point in the history
…ackducksoftware#60)

* Added a new function to create HubClientErr with HTTP status code
* made statusCode first parameter to separate it from format strings args

Co-authored-by: Tarun Sethi <[email protected]>
  • Loading branch information
tarunsethi and Tarun Sethi authored Jul 29, 2020
1 parent 5adee8f commit e3097bb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hubclient/apitoken-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewWithApiTokenAndClient(baseURL string, apiToken string, debugFlags HubCli
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, HubClientErrorf("got a %d response instead of a %d", resp.StatusCode, http.StatusOK)
return nil, HubClientStatusCodeErrorf(resp.StatusCode, "got a %d response instead of a %d", resp.StatusCode, http.StatusOK)
}

csrf := resp.Header.Get(HeaderNameCsrfToken)
Expand Down
7 changes: 6 additions & 1 deletion hubclient/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ func TraceHubClientError(old error) error {
}

func HubClientErrorf(format string, args ...interface{}) error {
return HubClientStatusCodeErrorf(0, format, args...)
}

func HubClientStatusCodeErrorf(statusCode int, format string, args ...interface{}) error {
newErr := errors.Errorf(format, args...)
err := &HubClientError{newErr, 0, HubResponseError{}}
err := &HubClientError{newErr, statusCode, HubResponseError{}}
return err
}

2 changes: 1 addition & 1 deletion hubclient/login-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *Client) Login(username string, password string) error {
}

if resp.StatusCode != 204 {
return HubClientErrorf("got a %d response instead of a 204", resp.StatusCode)
return HubClientStatusCodeErrorf(resp.StatusCode, "got a %d response instead of a 204", resp.StatusCode)
}

if csrf := resp.Header.Get(HeaderNameCsrfToken); csrf != "" {
Expand Down
2 changes: 1 addition & 1 deletion hubclient/scanclient-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *Client) downloadScanClientHelper(path string, urlPath string) error {
if err != nil {
return AnnotateHubClientErrorf(err, "unable to http GET %s", urlPath)
} else if resp.StatusCode != http.StatusOK {
return HubClientErrorf("GET failed: received status != 200 from %s: %s", scanClientURL, resp.Status)
return HubClientStatusCodeErrorf(resp.StatusCode, "GET failed: received status != 200 from %s: %s", scanClientURL, resp.Status)
}

body := resp.Body
Expand Down

0 comments on commit e3097bb

Please sign in to comment.