Skip to content

Commit c6acf18

Browse files
committedJul 16, 2024·
Avoid squashing http response err info with unmarshal err
1 parent 1699359 commit c6acf18

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed
 

Diff for: ‎pkg/utils/util.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,25 @@ func MatchBaseDomain(longHostname, baseDomain string) bool {
105105
}
106106

107107
func TryParseBackplaneAPIError(rsp *http.Response) (*BackplaneApi.Error, error) {
108+
if rsp == nil {
109+
return nil, fmt.Errorf("parse err provided nil http response")
110+
}
108111
bodyBytes, err := io.ReadAll(rsp.Body)
109-
defer func() { _ = rsp.Body.Close() }()
112+
defer func() {
113+
_ = rsp.Body.Close()
114+
}()
110115
if err != nil {
111116
return nil, err
117+
} else {
118+
var dest BackplaneApi.Error
119+
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
120+
// Avoid squashing the HTTP response info with Unmarshal err...
121+
bodyStr := strings.ReplaceAll(string(bodyBytes[:]), "\n", " ")
122+
err := fmt.Errorf("status:'%s', code:'%d'; failed to unmarshal response:'%s'; %w", rsp.Status, rsp.StatusCode, bodyStr, err)
123+
return nil, err
124+
}
125+
return &dest, nil
112126
}
113-
114-
var dest BackplaneApi.Error
115-
116-
if err := json.Unmarshal(bodyBytes, &dest); err != nil {
117-
return nil, err
118-
}
119-
120-
return &dest, nil
121127
}
122128

123129
func TryRenderErrorRaw(rsp *http.Response) error {

0 commit comments

Comments
 (0)
Please sign in to comment.