Skip to content

Commit e998d6b

Browse files
committed
Refactor TranslateStatusCode to accept http.Response as parameter
1 parent a388d94 commit e998d6b

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

errors/http_error_handling.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ func HandleAPIError(resp *http.Response, log logger.Logger) error {
7474
}
7575

7676
// TranslateStatusCode provides a human-readable message for HTTP status codes.
77-
func TranslateStatusCode(statusCode int) string {
77+
func TranslateStatusCode(resp *http.Response) string {
78+
79+
if resp == nil {
80+
return "No status code received, possible network or connection error."
81+
}
82+
7883
messages := map[int]string{
7984
http.StatusOK: "Request successful.",
8085
http.StatusCreated: "Request to create or update resource successful.",
@@ -115,7 +120,8 @@ func TranslateStatusCode(statusCode int) string {
115120
http.StatusNetworkAuthenticationRequired: "Network authentication required. The client needs to authenticate to gain network access.",
116121
}
117122

118-
if message, exists := messages[statusCode]; exists {
123+
// Lookup and return the message for the given status code
124+
if message, exists := messages[resp.StatusCode]; exists {
119125
return message
120126
}
121127
return "An unexpected error occurred. Please try again later."

httpclient/httpclient_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (c *Client) executeRequestWithRetries(method, endpoint string, body, out in
163163
}
164164

165165
// Leverage TranslateStatusCode for more descriptive error logging
166-
statusMessage := errors.TranslateStatusCode(resp.StatusCode)
166+
statusMessage := errors.TranslateStatusCode(resp)
167167

168168
// Check for non-retryable errors
169169
if resp != nil && errors.IsNonRetryableStatusCode(resp) {

0 commit comments

Comments
 (0)