From 7348b338367af2f5cd8e02565bc581f59e9aceaa Mon Sep 17 00:00:00 2001 From: Geoff Greer Date: Tue, 30 Jul 2024 07:50:20 -0700 Subject: [PATCH] Handle case of resp possibly being nil due. --- pkg/client/client.go | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index af53de91..cb2a7d80 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -206,13 +206,16 @@ func (d *DataCenterClient) CachedGet(ctx context.Context, req *http.Request, opt l.Debug("cache miss", zap.String("cacheKey", cacheKey)) resp, err := d.httpClient.Do(req, options...) if err != nil { - return nil, &BitbucketError{ + bbErr := &BitbucketError{ ErrorMessage: err.Error(), ErrorDescription: err.Error(), - ErrorCode: resp.StatusCode, - ErrorSummary: fmt.Sprint(resp.Body), ErrorLink: req.URL.String(), } + if resp != nil { + bbErr.ErrorCode = resp.StatusCode + bbErr.ErrorSummary = fmt.Sprint(resp.Body) + } + return nil, bbErr } d.bitbucketCache.Set(cacheKey, resp) @@ -1209,13 +1212,16 @@ func (d *DataCenterClient) UpdateGroupRepositoryPermission(ctx context.Context, resp, err := d.httpClient.Do(req) if err != nil { - return &BitbucketError{ + bbErr := &BitbucketError{ ErrorMessage: err.Error(), ErrorDescription: err.Error(), - ErrorCode: resp.StatusCode, - ErrorSummary: fmt.Sprint(resp.Body), ErrorLink: endpointUrl, } + if resp != nil { + bbErr.ErrorCode = resp.StatusCode + bbErr.ErrorSummary = fmt.Sprint(resp.Body) + } + return bbErr } defer resp.Body.Close() @@ -1373,13 +1379,16 @@ func (d *DataCenterClient) RevokeGroupProjectPermission(ctx context.Context, pro resp, err := d.httpClient.Do(req) if err != nil { - return &BitbucketError{ + bbErr := &BitbucketError{ ErrorMessage: err.Error(), ErrorDescription: err.Error(), - ErrorCode: resp.StatusCode, - ErrorSummary: fmt.Sprint(resp.Body), ErrorLink: endpointUrl, } + if resp != nil { + bbErr.ErrorCode = resp.StatusCode + bbErr.ErrorSummary = fmt.Sprint(resp.Body) + } + return bbErr } defer resp.Body.Close() @@ -1459,13 +1468,16 @@ func (d *DataCenterClient) UpdateGroupProjectPermission(ctx context.Context, pro resp, err := d.httpClient.Do(req) if err != nil { - return &BitbucketError{ + bbErr := &BitbucketError{ ErrorMessage: err.Error(), ErrorDescription: err.Error(), - ErrorCode: resp.StatusCode, - ErrorSummary: fmt.Sprint(resp.Body), ErrorLink: endpointUrl, } + if resp != nil { + bbErr.ErrorCode = resp.StatusCode + bbErr.ErrorSummary = fmt.Sprint(resp.Body) + } + return bbErr } defer resp.Body.Close()