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

Dev #92

Merged
merged 3 commits into from
Feb 29, 2024
Merged

Dev #92

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
26 changes: 8 additions & 18 deletions errors/http_error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
}

// HandleAPIError handles error responses from the API, converting them into a structured error if possible.
func HandleAPIError(resp *http.Response, log logger.Logger) error {
func HandleAPIError(resp *http.Response, log logger.Logger) *APIError {
var structuredErr StructuredError
err := json.NewDecoder(resp.Body).Decode(&structuredErr)
if err == nil && structuredErr.Error.Message != "" {
// Using structured logging to log the structured error details
// Log the structured error details
log.Warn("API returned structured error",
zap.String("status", resp.Status),
zap.String("error_code", structuredErr.Error.Code),
Expand All @@ -49,22 +49,12 @@
}
}

var errMsg string
err = json.NewDecoder(resp.Body).Decode(&errMsg)
if err != nil || errMsg == "" {
errMsg = fmt.Sprintf("Unexpected error with status code: %d", resp.StatusCode)
// Logging with structured fields
log.Error("Failed to decode API error message, using default error message",
zap.String("status", resp.Status),
zap.String("error_message", errMsg),
)
} else {
// Logging non-structured error as a warning with structured fields
log.Warn("API returned non-structured error",
zap.String("status", resp.Status),
zap.String("error_message", errMsg),
)
}
// Default error message for non-structured responses or decode failures
errMsg := fmt.Sprintf("Unexpected error with status code: %d", resp.StatusCode)
log.Error("Failed to decode API error message, using default error message",
zap.String("status", resp.Status),
zap.String("error_message", errMsg),
)
Comment on lines +54 to +57

Check warning

Code scanning / gosec

Errors unhandled. Warning

Errors unhandled.

return &APIError{
StatusCode: resp.StatusCode,
Expand Down
4 changes: 2 additions & 2 deletions httpclient/httpclient_auth_token_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (c *Client) ValidAuthTokenCheck() (bool, error) {
log.Info("Credential Match", zap.String("AuthMethod", c.AuthMethod))
err := c.ObtainToken(log)
if err != nil {
return false, log.Error("Failed to obtain bearer token", zap.Error(err))
return false, log.Error("Bearer token retrieval failed: invalid credentials. Verify accuracy.", zap.Error(err))
}
} else if c.AuthMethod == "oauth" {
log.Info("Credential Match", zap.String("AuthMethod", c.AuthMethod))
if err := c.ObtainOAuthToken(c.clientConfig.Auth); err != nil {
return false, log.Error("Failed to obtain OAuth token", zap.Error(err))
return false, log.Error("OAuth token retrieval failed: invalid credentials. Verify accuracy.", zap.Error(err))
}
} else {
return false, log.Error("No valid credentials provided. Unable to obtain a token", zap.String("authMethod", c.AuthMethod))
Expand Down
188 changes: 0 additions & 188 deletions httpclient/httpclient_client.go.back

This file was deleted.

Loading