partital fix - checkpoint 3 - need help

This commit is contained in:
2025-08-28 17:42:33 -07:00
parent ff5065770a
commit 6aadcad71d
2 changed files with 14 additions and 6 deletions

View File

@@ -122,14 +122,21 @@ func (c *Client) refreshTokenIfNeeded() error {
// handleAPIError processes API errors including JSON unmarshaling issues
func handleAPIError(resp *resty.Response) error {
// Check if response has valid JSON error structure
errorResponse := struct {
// First try to parse as standard Garmin error format
standardError := struct {
Code int `json:"code"`
Message string `json:"message"`
}{}
if err := json.Unmarshal(resp.Body(), &standardError); err == nil && standardError.Code != 0 {
return fmt.Errorf("API error %d: %s", standardError.Code, standardError.Message)
}
if err := json.Unmarshal(resp.Body(), &errorResponse); err == nil {
return fmt.Errorf("API error %d: %s", errorResponse.Code, errorResponse.Message)
// Try to parse as alternative error format
altError := struct {
Error string `json:"error"`
}{}
if err := json.Unmarshal(resp.Body(), &altError); err == nil && altError.Error != "" {
return fmt.Errorf("API error %d: %s", resp.StatusCode(), altError.Error)
}
// Check for unmarshaling errors in successful responses

View File

@@ -86,8 +86,9 @@ func TestGetUserProfile(t *testing.T) {
profile, err := client.GetUserProfile(context.Background())
if tt.expectedError != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedError)
if assert.Error(t, err) {
assert.Contains(t, err.Error(), tt.expectedError)
}
assert.Nil(t, profile)
} else {
assert.NoError(t, err)