Skip to content

Commit cf2ebe0

Browse files
Copilotcgillum
andcommitted
Refactor error handling to reduce code duplication
Co-authored-by: cgillum <2704139+cgillum@users.noreply.github.com>
1 parent 91c36f1 commit cf2ebe0

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

azure/durable_functions/models/utils/http_utils.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ async def _get_session() -> aiohttp.ClientSession:
4848
return _client_session
4949

5050

51+
async def _handle_request_error():
52+
"""Handle connection errors by closing and resetting the session.
53+
54+
This handles cases where the remote host process recycles.
55+
"""
56+
global _client_session
57+
if _client_session is not None and not _client_session.closed:
58+
await _client_session.close()
59+
_client_session = None
60+
61+
5162
async def _close_session() -> None:
5263
"""Close the shared ClientSession if it exists.
5364
@@ -99,11 +110,7 @@ async def post_async_request(url: str,
99110
return [response.status, data]
100111
except (aiohttp.ClientError, asyncio.TimeoutError):
101112
# On connection errors, close and recreate session for next request
102-
# This handles cases where the remote host process recycles
103-
global _client_session
104-
if _client_session is not None and not _client_session.closed:
105-
await _client_session.close()
106-
_client_session = None
113+
await _handle_request_error()
107114
raise
108115

109116

@@ -130,11 +137,7 @@ async def get_async_request(url: str) -> List[Any]:
130137
return [response.status, data]
131138
except (aiohttp.ClientError, asyncio.TimeoutError):
132139
# On connection errors, close and recreate session for next request
133-
# This handles cases where the remote host process recycles
134-
global _client_session
135-
if _client_session is not None and not _client_session.closed:
136-
await _client_session.close()
137-
_client_session = None
140+
await _handle_request_error()
138141
raise
139142

140143

@@ -159,9 +162,5 @@ async def delete_async_request(url: str) -> List[Union[int, Any]]:
159162
return [response.status, data]
160163
except (aiohttp.ClientError, asyncio.TimeoutError):
161164
# On connection errors, close and recreate session for next request
162-
# This handles cases where the remote host process recycles
163-
global _client_session
164-
if _client_session is not None and not _client_session.closed:
165-
await _client_session.close()
166-
_client_session = None
165+
await _handle_request_error()
167166
raise

0 commit comments

Comments
 (0)