From 3aba5e757776f5134b8bc4b09f97ca9bb0217e3b Mon Sep 17 00:00:00 2001 From: Pawel Kosiec Date: Mon, 20 Nov 2023 13:21:59 +0100 Subject: [PATCH] Add helper funcs for HTTP errors --- connector/client/client.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/connector/client/client.go b/connector/client/client.go index b44d5c1..3842a8d 100644 --- a/connector/client/client.go +++ b/connector/client/client.go @@ -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() @@ -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() @@ -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) { @@ -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() @@ -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, + } +}