mirror of
https://github.com/sstent/go-garminconnect.git
synced 2026-02-16 12:15:34 +00:00
partital fix - checkpoint 3 - need help
This commit is contained in:
@@ -122,14 +122,21 @@ func (c *Client) refreshTokenIfNeeded() error {
|
|||||||
|
|
||||||
// handleAPIError processes API errors including JSON unmarshaling issues
|
// handleAPIError processes API errors including JSON unmarshaling issues
|
||||||
func handleAPIError(resp *resty.Response) error {
|
func handleAPIError(resp *resty.Response) error {
|
||||||
// Check if response has valid JSON error structure
|
// First try to parse as standard Garmin error format
|
||||||
errorResponse := struct {
|
standardError := struct {
|
||||||
Code int `json:"code"`
|
Code int `json:"code"`
|
||||||
Message string `json:"message"`
|
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 {
|
// Try to parse as alternative error format
|
||||||
return fmt.Errorf("API error %d: %s", errorResponse.Code, errorResponse.Message)
|
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
|
// Check for unmarshaling errors in successful responses
|
||||||
|
|||||||
@@ -86,8 +86,9 @@ func TestGetUserProfile(t *testing.T) {
|
|||||||
profile, err := client.GetUserProfile(context.Background())
|
profile, err := client.GetUserProfile(context.Background())
|
||||||
|
|
||||||
if tt.expectedError != "" {
|
if tt.expectedError != "" {
|
||||||
assert.Error(t, err)
|
if assert.Error(t, err) {
|
||||||
assert.Contains(t, err.Error(), tt.expectedError)
|
assert.Contains(t, err.Error(), tt.expectedError)
|
||||||
|
}
|
||||||
assert.Nil(t, profile)
|
assert.Nil(t, profile)
|
||||||
} else {
|
} else {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
Reference in New Issue
Block a user