Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improve API error handling #4565

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pkg/publicapi/apimodels/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package apimodels

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -67,31 +66,37 @@ func (e *APIError) Error() string {
}

// Parse HTTP Resposne to APIError
func FromHttpResponse(resp *http.Response) (*APIError, error) {

func GenerateAPIErrorFromHTTPResponse(resp *http.Response) *APIError {
if resp == nil {
return nil, errors.New("response is nil, cannot be unmarsheld to APIError")
return NewAPIError(0, "API call error, invalid response")
}

defer resp.Body.Close()
jamlo marked this conversation as resolved.
Show resolved Hide resolved

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
return NewAPIError(
resp.StatusCode,
fmt.Sprintf("Unable to read API call response body. Error: %q", err.Error()))
}

var apiErr APIError
err = json.Unmarshal(body, &apiErr)
if err != nil {
return nil, fmt.Errorf("error parsing response body: %w", err)
return NewAPIError(
resp.StatusCode,
fmt.Sprintf("Unable to parse API call response body. Error: %q. Body received: %q",
err.Error(),
string(body),
))
}

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

return &apiErr, nil
return &apiErr
}

// FromBacError converts a bacerror.Error to an APIError
Expand Down
27 changes: 8 additions & 19 deletions pkg/publicapi/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,12 @@ func (c *httpClient) Get(ctx context.Context, endpoint string, in apimodels.GetR
return apimodels.NewUnauthorizedError("invalid token")
}

var apiError *apimodels.APIError
if resp.StatusCode != http.StatusOK {
apiError, err = apimodels.FromHttpResponse(resp)
if err != nil {
return err
if apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp); apiError != nil {
return apiError
}
}

if apiError != nil {
return apiError
}

defer resp.Body.Close()

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

var apiError *apimodels.APIError
if resp.StatusCode != http.StatusOK {
apiError, err = apimodels.FromHttpResponse(resp)
if err != nil {
return err
if apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp); apiError != nil {
return apiError
}
}

if apiError != nil {
return apiError
}

if out != nil {
if err := decodeBody(resp, &out); err != nil {
return err
Expand Down Expand Up @@ -361,12 +349,13 @@ func (c *httpClient) interceptError(ctx context.Context, err error, resp *http.R
WithCode(bacerrors.UnauthorizedError)
}

apiError, apiErr := apimodels.FromHttpResponse(resp)
if apiErr == nil {
apiError := apimodels.GenerateAPIErrorFromHTTPResponse(resp)
if apiError != nil {
return apiError.ToBacError()
}

return bacerrors.Wrap(apiErr, "server error").
return bacerrors.New("server error").
WithHTTPStatusCode(http.StatusInternalServerError).
WithCode(bacerrors.InternalError)
}

Expand Down
Loading