Skip to content

Commit 2252aca

Browse files
author
Tjaz Erzen
committed
Simplify handling of retry logic
1 parent 55ddd34 commit 2252aca

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

codeplain_REST_api.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _extend_payload_with_run_state(self, payload: dict, run_state: RunState):
4444
run_state.increment_call_count()
4545
payload["render_state"] = run_state.to_dict()
4646

47-
def _handle_retry_logic(self, attempt: int, retry_delay: int, error: Exception, error_type: str = "Error") -> int:
47+
def _handle_retry_logic(self, attempt: int, retry_delay: int, error: Exception) -> int:
4848
"""
4949
Handles retry logic with exponential backoff.
5050
@@ -60,15 +60,20 @@ def _handle_retry_logic(self, attempt: int, retry_delay: int, error: Exception,
6060
Raises:
6161
The original exception if max retries exceeded
6262
"""
63+
is_connection_error = isinstance(error, ConnectionError) or isinstance(error, Timeout)
64+
connection_error_type = "Network error" if is_connection_error else "Error"
6365
if attempt < MAX_RETRIES:
64-
self.console.info(f"{error_type} on attempt {attempt + 1}/{MAX_RETRIES + 1}: {error}")
66+
self.console.info(f"{connection_error_type} on attempt {attempt + 1}/{MAX_RETRIES + 1}: {error}")
6567
self.console.info(f"Retrying in {retry_delay} seconds...")
6668
time.sleep(retry_delay)
6769
# Exponential backoff
6870
return retry_delay * 2
6971
else:
7072
self.console.error(f"Max retries ({MAX_RETRIES}) exceeded. Last error: {error}")
71-
raise error
73+
if is_connection_error:
74+
raise plain2code_exceptions.NetworkConnectionError("Failed to connect to API server.")
75+
else:
76+
raise error
7277

7378
def _raise_for_error_code(self, response_json):
7479
"""Raise appropriate exception based on error code in response."""
@@ -110,17 +115,6 @@ def post_request(
110115
response.raise_for_status()
111116
return response_json
112117

113-
except (ConnectionError, Timeout) as e:
114-
# Network-related errors should always be retried
115-
if attempt < MAX_RETRIES:
116-
retry_delay = self._handle_retry_logic(attempt, retry_delay, e, "Network error")
117-
else:
118-
self.console.error(f"Max retries ({MAX_RETRIES}) exceeded. Network connection failed.")
119-
self.console.error(f"Last error: {str(e)}")
120-
raise plain2code_exceptions.NetworkConnectionError(
121-
f"Failed to connect to API server after {MAX_RETRIES + 1} attempts. "
122-
)
123-
124118
except Exception as e:
125119
# For other errors, check if they should be retried
126120
if response_json is not None and "error_code" in response_json:

0 commit comments

Comments
 (0)