mirror of
https://github.com/sstent/go-garminconnect.git
synced 2026-01-25 16:42:32 +00:00
env working but auth not yet
This commit is contained in:
@@ -54,6 +54,11 @@ func (c *Client) Get(ctx context.Context, path string, v interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Handle unmarshaling errors for successful responses
|
||||
if resp.IsSuccess() && resp.Error() != nil {
|
||||
return handleAPIError(resp)
|
||||
}
|
||||
|
||||
if resp.StatusCode() == http.StatusUnauthorized {
|
||||
// Force token refresh on next attempt
|
||||
c.session = nil
|
||||
@@ -79,6 +84,11 @@ func (c *Client) Post(ctx context.Context, path string, body interface{}, v inte
|
||||
return err
|
||||
}
|
||||
|
||||
// Handle unmarshaling errors for successful responses
|
||||
if resp.IsSuccess() && resp.Error() != nil {
|
||||
return handleAPIError(resp)
|
||||
}
|
||||
|
||||
if resp.StatusCode() >= 400 {
|
||||
return handleAPIError(resp)
|
||||
}
|
||||
@@ -110,8 +120,9 @@ func (c *Client) refreshTokenIfNeeded() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleAPIError processes non-200 responses
|
||||
// handleAPIError processes API errors including JSON unmarshaling issues
|
||||
func handleAPIError(resp *resty.Response) error {
|
||||
// Check if response has valid JSON error structure
|
||||
errorResponse := struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
@@ -121,5 +132,10 @@ func handleAPIError(resp *resty.Response) error {
|
||||
return fmt.Errorf("API error %d: %s", errorResponse.Code, errorResponse.Message)
|
||||
}
|
||||
|
||||
// Check for unmarshaling errors in successful responses
|
||||
if resp.IsSuccess() {
|
||||
return fmt.Errorf("failed to unmarshal successful response: %w", json.Unmarshal(resp.Body(), nil))
|
||||
}
|
||||
|
||||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user