Skip to content

Commit

Permalink
merged status package into response. They seem like the same subject?
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeker12 committed Jul 9, 2024
1 parent 2d37ff9 commit 482eb5a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 78 deletions.
6 changes: 3 additions & 3 deletions httpclient/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
}

// Non Retry
if IsNonRetryableStatusCode(resp.StatusCode) {
if response.IsNonRetryableStatusCode(resp.StatusCode) {
c.Sugar.Warn("Non-retryable error received", zap.Int("status_code", resp.StatusCode), zap.String("status_message", statusMessage))
return resp, response.HandleAPIErrorResponse(resp, c.Sugar)
}
Expand All @@ -165,7 +165,7 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
}

// Transient
if IsTransientError(resp) {
if response.IsTransientError(resp.StatusCode) {
retryCount++
if retryCount > c.config.MaxRetryAttempts {
c.Sugar.Warn("Max retry attempts reached", zap.String("method", method), zap.String("endpoint", endpoint))
Expand All @@ -178,7 +178,7 @@ func (c *Client) requestWithRetries(method, endpoint string, body, out interface
}

// Retryable
if !IsRetryableStatusCode(resp.StatusCode) {
if !response.IsRetryableStatusCode(resp.StatusCode) {
if apiErr := response.HandleAPIErrorResponse(resp, c.Sugar); apiErr != nil {
err = apiErr
}
Expand Down
62 changes: 0 additions & 62 deletions httpclient/status.go

This file was deleted.

23 changes: 10 additions & 13 deletions status/status.go → response/status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// status.go
// This package provides utility functions and structures for handling and categorizing HTTP error responses.
package status
package response

import (
"net/http"
Expand All @@ -24,9 +24,9 @@ func IsRedirectStatusCode(statusCode int) bool {
http.StatusTemporaryRedirect,
http.StatusPermanentRedirect:
return true
default:
return false
}

return false
}

// IsPermanentRedirect checks if the provided HTTP status code is one of the permanent redirect codes.
Expand All @@ -35,13 +35,13 @@ func IsPermanentRedirect(statusCode int) bool {
case http.StatusMovedPermanently,
http.StatusPermanentRedirect:
return true
default:
return false
}

return false
}

// IsNonRetryableStatusCode checks if the provided response indicates a non-retryable error.
func IsNonRetryableStatusCode(resp *http.Response) bool {
func IsNonRetryableStatusCode(statusCode int) bool {
nonRetryableStatusCodes := map[int]bool{
http.StatusBadRequest: true,
http.StatusUnauthorized: true,
Expand All @@ -68,20 +68,18 @@ func IsNonRetryableStatusCode(resp *http.Response) bool {
http.StatusRequestHeaderFieldsTooLarge: true,
http.StatusUnavailableForLegalReasons: true,
}

_, isNonRetryable := nonRetryableStatusCodes[resp.StatusCode]
return isNonRetryable
return nonRetryableStatusCodes[statusCode]
}

// IsTransientError checks if an error or HTTP response indicates a transient error.
func IsTransientError(resp *http.Response) bool {
func IsTransientError(statusCode int) bool {
transientStatusCodes := map[int]bool{
http.StatusInternalServerError: true,
http.StatusBadGateway: true,
http.StatusServiceUnavailable: true,
http.StatusGatewayTimeout: true,
}
return resp != nil && transientStatusCodes[resp.StatusCode]
return transientStatusCodes[statusCode]
}

// IsRetryableStatusCode checks if the provided HTTP status code is considered retryable.
Expand All @@ -95,6 +93,5 @@ func IsRetryableStatusCode(statusCode int) bool {
http.StatusGatewayTimeout: true,
}

_, retryable := retryableStatusCodes[statusCode]
return retryable
return retryableStatusCodes[statusCode]
}

0 comments on commit 482eb5a

Please sign in to comment.