Skip to content

Commit

Permalink
Merge pull request #485 from nephomaniac/parse_api_error
Browse files Browse the repository at this point in the history
Avoid squashing http response err info with unmarshal err
  • Loading branch information
openshift-merge-bot[bot] committed Jul 16, 2024
2 parents 1699359 + c6acf18 commit 272df9a
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,25 @@ func MatchBaseDomain(longHostname, baseDomain string) bool {
}

func TryParseBackplaneAPIError(rsp *http.Response) (*BackplaneApi.Error, error) {
if rsp == nil {
return nil, fmt.Errorf("parse err provided nil http response")
}
bodyBytes, err := io.ReadAll(rsp.Body)
defer func() { _ = rsp.Body.Close() }()
defer func() {
_ = rsp.Body.Close()
}()
if err != nil {
return nil, err
} else {
var dest BackplaneApi.Error
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
// Avoid squashing the HTTP response info with Unmarshal err...
bodyStr := strings.ReplaceAll(string(bodyBytes[:]), "\n", " ")
err := fmt.Errorf("status:'%s', code:'%d'; failed to unmarshal response:'%s'; %w", rsp.Status, rsp.StatusCode, bodyStr, err)
return nil, err
}
return &dest, nil
}

var dest BackplaneApi.Error

if err := json.Unmarshal(bodyBytes, &dest); err != nil {
return nil, err
}

return &dest, nil
}

func TryRenderErrorRaw(rsp *http.Response) error {
Expand Down

0 comments on commit 272df9a

Please sign in to comment.