env working but auth not yet

This commit is contained in:
2025-08-28 14:43:18 -07:00
parent 05eb95304e
commit 5f27c27444
5 changed files with 81 additions and 95 deletions

View File

@@ -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())
}