Skip to content

Commit cf9789e

Browse files
committed
Slightly improve API error handling
This commit tries to fix the "FromHttpResponse" function. Returning 2 errors from a function can be confusing and uncommon in Golang, especially if the function only returns these 2 errors. The new code code will return one error, which is always an APIError, and it also improves the error messaging wording, adding more details in the output. Several more tweaks can be done to the current error message handling if needed.
1 parent 329319b commit cf9789e

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

pkg/publicapi/apimodels/error.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package apimodels
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"io"
87
"net/http"
@@ -67,31 +66,37 @@ func (e *APIError) Error() string {
6766
}
6867

6968
// Parse HTTP Resposne to APIError
70-
func FromHttpResponse(resp *http.Response) (*APIError, error) {
71-
69+
func GenerateAPIErrorFromHTTPResponse(resp *http.Response) *APIError {
7270
if resp == nil {
73-
return nil, errors.New("response is nil, cannot be unmarsheld to APIError")
71+
return NewAPIError(0, "API call error, invalid response")
7472
}
7573

7674
defer resp.Body.Close()
7775

7876
body, err := io.ReadAll(resp.Body)
7977
if err != nil {
80-
return nil, fmt.Errorf("error reading response body: %w", err)
78+
return NewAPIError(
79+
resp.StatusCode,
80+
fmt.Sprintf("Unable to read API response body. Error: %q", err.Error()))
8181
}
8282

8383
var apiErr APIError
8484
err = json.Unmarshal(body, &apiErr)
8585
if err != nil {
86-
return nil, fmt.Errorf("error parsing response body: %w", err)
86+
return NewAPIError(
87+
resp.StatusCode,
88+
fmt.Sprintf("Unable to parse API response body. Error: %q. Body received: %q",
89+
err.Error(),
90+
string(body),
91+
))
8792
}
8893

8994
// If the JSON didn't include a status code, use the HTTP Status
9095
if apiErr.HTTPStatusCode == 0 {
9196
apiErr.HTTPStatusCode = resp.StatusCode
9297
}
9398

94-
return &apiErr, nil
99+
return &apiErr
95100
}
96101

97102
// FromBacError converts a bacerror.Error to an APIError

pkg/publicapi/client/v2/client.go

+8-19
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,12 @@ func (c *httpClient) Get(ctx context.Context, endpoint string, in apimodels.GetR
7373
return apimodels.NewUnauthorizedError("invalid token")
7474
}
7575

76-
var apiError *apimodels.APIError
7776
if resp.StatusCode != http.StatusOK {
78-
apiError, err = apimodels.FromHttpResponse(resp)
79-
if err != nil {
80-
return err
77+
if apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp); apiError != nil {
78+
return apiError
8179
}
8280
}
8381

84-
if apiError != nil {
85-
return apiError
86-
}
87-
8882
defer resp.Body.Close()
8983

9084
if out != nil {
@@ -115,18 +109,12 @@ func (c *httpClient) write(ctx context.Context, verb, endpoint string, in apimod
115109
return apimodels.ErrInvalidToken
116110
}
117111

118-
var apiError *apimodels.APIError
119112
if resp.StatusCode != http.StatusOK {
120-
apiError, err = apimodels.FromHttpResponse(resp)
121-
if err != nil {
122-
return err
113+
if apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp); apiError != nil {
114+
return apiError
123115
}
124116
}
125117

126-
if apiError != nil {
127-
return apiError
128-
}
129-
130118
if out != nil {
131119
if err := decodeBody(resp, &out); err != nil {
132120
return err
@@ -361,12 +349,13 @@ func (c *httpClient) interceptError(ctx context.Context, err error, resp *http.R
361349
WithCode(bacerrors.UnauthorizedError)
362350
}
363351

364-
apiError, apiErr := apimodels.FromHttpResponse(resp)
365-
if apiErr == nil {
352+
apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp)
353+
if apiError != nil {
366354
return apiError.ToBacError()
367355
}
368356

369-
return bacerrors.Wrap(apiErr, "server error").
357+
return bacerrors.New("server error").
358+
WithHTTPStatusCode(http.StatusInternalServerError).
370359
WithCode(bacerrors.InternalError)
371360
}
372361

0 commit comments

Comments
 (0)