Skip to content

Commit

Permalink
Ensure all HubClientError instances have the underlying cause if ther…
Browse files Browse the repository at this point in the history
…e is one
  • Loading branch information
Tarun Sethi authored and tandr committed Aug 24, 2020
1 parent e3097bb commit fd8d337
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*~
/hubclient/testserver.json
/hubclient/testserver.json

# All of .idea folder
/.idea/*
4 changes: 2 additions & 2 deletions hubclient/apitoken-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func NewWithApiTokenAndClient(baseURL string, apiToken string, debugFlags HubCli

csrf := resp.Header.Get(HeaderNameCsrfToken)
if csrf == "" {
return nil, newHubClientError(nil, resp, "CSRF token not found")
return nil, newHubClientError(nil, resp, "CSRF token not found", nil)
}

body, err := ioutil.ReadAll(resp.Body)
Expand All @@ -79,7 +79,7 @@ func NewWithApiTokenAndClient(baseURL string, apiToken string, debugFlags HubCli
}

if bearerTokenResponse.BearerToken == "" {
return nil, newHubClientError(body, resp, "bearer token not found")
return nil, newHubClientError(body, resp, "bearer token not found", nil)
}

log.Debug("Logged in with auth token successfully")
Expand Down
46 changes: 28 additions & 18 deletions hubclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func validateHTTPResponse(resp *http.Response, expectedStatusCode int, debugFlag

if statusCode != expectedStatusCode { // Should this be a list at some point?
body := readResponseBody(resp, debugFlags)
return newHubClientError(body, resp, fmt.Sprintf("got a %d response instead of a %d", statusCode, expectedStatusCode))
return newHubClientError(body, resp, fmt.Sprintf("got a %d response instead of a %d", statusCode, expectedStatusCode), nil)
}

return nil
Expand All @@ -157,13 +157,13 @@ func (c *Client) processResponse(resp *http.Response, result interface{}, expect

bodyBytes, err := readBytes(resp.Body, resp.ContentLength)
if err != nil {
return newHubClientError(bodyBytes, resp, fmt.Sprintf("error reading HTTP Response: %+v", err))
return newHubClientError(bodyBytes, resp, fmt.Sprintf("error reading HTTP Response: %+v", err), err)
}

debugReportBytes(bodyBytes, c.debugFlags)

if err := json.Unmarshal(bodyBytes, result); err != nil {
return newHubClientError(bodyBytes, resp, fmt.Sprintf("error parsing HTTP Response: %+v", err))
return newHubClientError(bodyBytes, resp, fmt.Sprintf("error parsing HTTP Response: %+v", err), err)
}

return nil
Expand All @@ -179,7 +179,7 @@ func (c *Client) HttpGetJSON(url string, result interface{}, expectedStatusCode

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return newHubClientError(nil, nil, fmt.Sprintf("error creating http get request for %s: %+v", url, err))
return newHubClientError(nil, nil, fmt.Sprintf("error creating http get request for %s: %+v", url, err), err)
}

c.applyHeaderValues(req)
Expand All @@ -197,7 +197,7 @@ func (c *Client) HttpGetJSON(url string, result interface{}, expectedStatusCode
httpStart := time.Now()
if resp, err = c.httpClient.Do(req); err != nil {
body := readResponseBody(resp, c.debugFlags)
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from %s: %+v", url, err))
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from %s: %+v", url, err), err)
}

if c.debugFlags&HubClientDebugTimings != 0 {
Expand All @@ -221,12 +221,12 @@ func (c *Client) HttpPutJSON(url string, data interface{}, contentType string, e
enc := json.NewEncoder(&buf)

if err := enc.Encode(&data); err != nil {
return newHubClientError(nil, nil, fmt.Sprintf("error encoding json: %+v", err))
return newHubClientError(nil, nil, fmt.Sprintf("error encoding json: %+v", err), err)
}

req, err := http.NewRequest(http.MethodPut, url, &buf)
if err != nil {
return newHubClientError(nil, nil, fmt.Sprintf("error creating http put request for %s: %+v", url, err))
return newHubClientError(nil, nil, fmt.Sprintf("error creating http put request for %s: %+v", url, err), err)
}

req.Header.Set(HeaderNameContentType, contentType)
Expand All @@ -237,7 +237,7 @@ func (c *Client) HttpPutJSON(url string, data interface{}, contentType string, e
httpStart := time.Now()
if resp, err = c.httpClient.Do(req); err != nil {
body := readResponseBody(resp, c.debugFlags)
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from PUT to %s: %+v", url, err))
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from PUT to %s: %+v", url, err), err)
}

if c.debugFlags&HubClientDebugTimings != 0 {
Expand All @@ -261,12 +261,12 @@ func (c *Client) HttpPostJSON(url string, data interface{}, contentType string,
enc := json.NewEncoder(&buf)

if err := enc.Encode(&data); err != nil {
return "", newHubClientError(nil, nil, fmt.Sprintf("error encoding json: %+v", err))
return "", newHubClientError(nil, nil, fmt.Sprintf("error encoding json: %+v", err), err)
}

req, err := http.NewRequest(http.MethodPost, url, &buf)
if err != nil {
return "", newHubClientError(nil, nil, fmt.Sprintf("error creating http post request for %s: %+v", url, err))
return "", newHubClientError(nil, nil, fmt.Sprintf("error creating http post request for %s: %+v", url, err), err)
}

req.Header.Set(HeaderNameContentType, contentType)
Expand All @@ -279,7 +279,7 @@ func (c *Client) HttpPostJSON(url string, data interface{}, contentType string,

if resp, err = c.httpClient.Do(req); err != nil {
body := readResponseBody(resp, c.debugFlags)
return "", newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from POST to %s: %+v", url, err))
return "", newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from POST to %s: %+v", url, err), err)
}

if c.debugFlags&HubClientDebugTimings != 0 {
Expand Down Expand Up @@ -307,12 +307,12 @@ func (c *Client) HttpPostJSONExpectResult(url string, data interface{}, result i
enc := json.NewEncoder(&buf)

if err := enc.Encode(&data); err != nil {
return "", newHubClientError(nil, nil, fmt.Sprintf("error encoding json: %+v", err))
return "", newHubClientError(nil, nil, fmt.Sprintf("error encoding json: %+v", err), err)
}

req, err := http.NewRequest(http.MethodPost, url, &buf)
if err != nil {
return "", newHubClientError(nil, nil, fmt.Sprintf("error creating http post request for %s: %+v", url, err))
return "", newHubClientError(nil, nil, fmt.Sprintf("error creating http post request for %s: %+v", url, err), err)
}

req.Header.Set(HeaderNameContentType, contentType)
Expand All @@ -324,7 +324,7 @@ func (c *Client) HttpPostJSONExpectResult(url string, data interface{}, result i
httpStart := time.Now()
if resp, err = c.httpClient.Do(req); err != nil {
body := readResponseBody(resp, c.debugFlags)
return "", newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from POST to %s: %+v", url, err))
return "", newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from POST to %s: %+v", url, err), err)
}

if c.debugFlags&HubClientDebugTimings != 0 {
Expand All @@ -350,7 +350,7 @@ func (c *Client) HttpDelete(url string, contentType string, expectedStatusCode i

req, err := http.NewRequest(http.MethodDelete, url, bytes.NewBuffer([]byte{}))
if err != nil {
return newHubClientError(nil, nil, fmt.Sprintf("error creating http delete request for %s: %+v", url, err))
return newHubClientError(nil, nil, fmt.Sprintf("error creating http delete request for %s: %+v", url, err), err)
}

req.Header.Set(HeaderNameContentType, contentType)
Expand All @@ -363,7 +363,7 @@ func (c *Client) HttpDelete(url string, contentType string, expectedStatusCode i

if resp, err = c.httpClient.Do(req); err != nil {
body := readResponseBody(resp, c.debugFlags)
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from DELETE to %s: %+v", url, err))
return newHubClientError(body, resp, fmt.Sprintf("error getting HTTP Response from DELETE to %s: %+v", url, err), err)
}

if c.debugFlags&HubClientDebugTimings != 0 {
Expand Down Expand Up @@ -477,7 +477,7 @@ func readResponseBody(resp *http.Response, debugFlags HubClientDebug) (bodyBytes
return bodyBytes
}

func newHubClientError(respBody []byte, resp *http.Response, message string) *HubClientError {
func newHubClientError(respBody []byte, resp *http.Response, message string, underlyingErr error) *HubClientError {
var hre HubResponseError

// Do not try to read the body of the response or response itself
Expand All @@ -486,7 +486,17 @@ func newHubClientError(respBody []byte, resp *http.Response, message string) *Hu
statusCode = resp.StatusCode
}

hce := &HubClientError{errors.New(message), statusCode, hre}
var err error
if underlyingErr == nil {
err = errors.New(message)
} else {
err = &errorWithMessage{
err: underlyingErr,
message: message,
}
}

hce := &HubClientError{err, statusCode, hre}
if len(respBody) > 0 {
if err := json.Unmarshal(respBody, &hre); err != nil {
hce = AnnotateHubClientError(hce, fmt.Sprintf("error unmarshaling HTTP response body: %+v", err)).(*HubClientError)
Expand Down
7 changes: 7 additions & 0 deletions hubclient/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,10 @@ func HubClientStatusCodeErrorf(statusCode int, format string, args ...interface{
return err
}

type errorWithMessage struct {
err error
message string
}

func (e *errorWithMessage) Error() string {return e.message}
func (e *errorWithMessage) Cause() error {return e.err}

0 comments on commit fd8d337

Please sign in to comment.