Skip to content

Commit

Permalink
Add helper funcs for HTTP errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Nov 20, 2023
1 parent 430aec7 commit 3aba5e7
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions connector/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ func (client *ConnectorClient) Get(ctx context.Context, target url.URL) (json.Ra

res, err := client.sendRequest(req)
if err != nil {
return nil, customerror.HTTPError{
HtErr: err,
}
return nil, newHTTPError(err)
}
defer res.Body.Close()

Expand Down Expand Up @@ -147,9 +145,7 @@ func (client *ConnectorClient) Put(ctx context.Context, target url.URL, activity
func (client *ConnectorClient) sendRequestWithRespErrCheck(req *http.Request) error {
res, err := client.sendRequest(req)
if err != nil {
return customerror.HTTPError{
HtErr: err,
}
return newHTTPError(err)
}

defer res.Body.Close()
Expand Down Expand Up @@ -177,10 +173,7 @@ func (client *ConnectorClient) checkRespError(resp *http.Response) error {
}
}

return customerror.HTTPError{
HtErr: errors.New("invalid response"),
StatusCode: resp.StatusCode,
}
return newHTTPErrorWithStatusCode(errors.New("invalid response"), resp.StatusCode)
}

func (client *ConnectorClient) getToken(ctx context.Context) (string, error) {
Expand Down Expand Up @@ -212,10 +205,7 @@ func (client *ConnectorClient) getToken(ctx context.Context) (string, error) {

resp, err := client.AuthClient.Do(r)
if err != nil {
return "", customerror.HTTPError{
StatusCode: resp.StatusCode,
HtErr: err,
}
return "", newHTTPErrorWithStatusCode(err, resp.StatusCode)
}

defer resp.Body.Close()
Expand All @@ -234,3 +224,16 @@ func (client *ConnectorClient) getToken(ctx context.Context) (string, error) {

return client.AuthCache.Keys.(string), nil
}

func newHTTPError(err error) error {
return customerror.HTTPError{
HtErr: err,
}
}

func newHTTPErrorWithStatusCode(err error, statusCode int) error {
return customerror.HTTPError{
HtErr: err,
StatusCode: statusCode,
}
}

0 comments on commit 3aba5e7

Please sign in to comment.