diff --git a/httpclient/request.go b/httpclient/request.go index dbcb123..e436938 100644 --- a/httpclient/request.go +++ b/httpclient/request.go @@ -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) } @@ -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)) @@ -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 } diff --git a/httpclient/status.go b/httpclient/status.go deleted file mode 100644 index 5e84211..0000000 --- a/httpclient/status.go +++ /dev/null @@ -1,62 +0,0 @@ -package httpclient - -import "net/http" - -// IsNonRetryableStatusCode checks if the provided response indicates a non-retryable error. -func IsNonRetryableStatusCode(statusCode int) bool { - nonRetryableStatusCodes := map[int]bool{ - http.StatusBadRequest: true, // 400 - Bad Request - http.StatusUnauthorized: true, // 401 - Unauthorized - http.StatusPaymentRequired: true, // 402 - Payment Required - http.StatusForbidden: true, // 403 - Forbidden - http.StatusNotFound: true, // 404 - Not Found - http.StatusMethodNotAllowed: true, // 405 - Method Not Allowed - http.StatusNotAcceptable: true, // 406 - Not Acceptable - http.StatusProxyAuthRequired: true, // 407 - Proxy Authentication Required - http.StatusConflict: true, // 409 - Conflict - http.StatusGone: true, // 410 - Gone - http.StatusLengthRequired: true, // 411 - Length Required - http.StatusPreconditionFailed: true, // 412 - Precondition Failed - http.StatusRequestEntityTooLarge: true, // 413 - Request Entity Too Large - http.StatusRequestURITooLong: true, // 414 - Request-URI Too Long - http.StatusUnsupportedMediaType: true, // 415 - Unsupported Media Type - http.StatusRequestedRangeNotSatisfiable: true, // 416 - Requested Range Not Satisfiable - http.StatusExpectationFailed: true, // 417 - Expectation Failed - http.StatusUnprocessableEntity: true, // 422 - Unprocessable Entity - http.StatusLocked: true, // 423 - Locked - http.StatusFailedDependency: true, // 424 - Failed Dependency - http.StatusUpgradeRequired: true, // 426 - Upgrade Required - http.StatusPreconditionRequired: true, // 428 - Precondition Required - http.StatusRequestHeaderFieldsTooLarge: true, // 431 - Request Header Fields Too Large - http.StatusUnavailableForLegalReasons: true, // 451 - Unavailable For Legal Reasons - } - - _, isNonRetryable := nonRetryableStatusCodes[statusCode] - return isNonRetryable -} - -// IsTransientError checks if an error or HTTP response indicates a transient error. -func IsTransientError(resp *http.Response) bool { - transientStatusCodes := map[int]bool{ - http.StatusInternalServerError: true, // 500 Internal Server Error - http.StatusBadGateway: true, // 502 Bad Gateway - http.StatusServiceUnavailable: true, // 503 Service Unavailable - http.StatusGatewayTimeout: true, // 504 - Gateway Timeout - } - return resp != nil && transientStatusCodes[resp.StatusCode] -} - -// IsRetryableStatusCode checks if the provided HTTP status code is considered retryable. -func IsRetryableStatusCode(statusCode int) bool { - retryableStatusCodes := map[int]bool{ - http.StatusRequestTimeout: true, // 408 - Request Timeout - http.StatusTooManyRequests: true, // 429 - http.StatusInternalServerError: true, // 500 - http.StatusBadGateway: true, // 502 - http.StatusServiceUnavailable: true, // 503 - http.StatusGatewayTimeout: true, // 504 - } - - _, retryable := retryableStatusCodes[statusCode] - return retryable -} diff --git a/status/status.go b/response/status.go similarity index 91% rename from status/status.go rename to response/status.go index 02fbb9d..f6bc058 100644 --- a/status/status.go +++ b/response/status.go @@ -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" @@ -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. @@ -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, @@ -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. @@ -95,6 +93,5 @@ func IsRetryableStatusCode(statusCode int) bool { http.StatusGatewayTimeout: true, } - _, retryable := retryableStatusCodes[statusCode] - return retryable + return retryableStatusCodes[statusCode] }