Skip to content

Commit df71d2e

Browse files
authored
fix(python-bindings): rename and improve TimeoutError and improve docstring (#180)
# Description - Do not overrride Python's built-in TimeoutError Exception - Created AGP custom exception from TimeoutError - General docstring fixes - Nit fix to raise a narrower exception Signed-off-by: brisacoder <[email protected]>
1 parent 3f005e5 commit df71d2e

File tree

1 file changed

+71
-19
lines changed

1 file changed

+71
-19
lines changed

data-plane/python-bindings/agp_bindings/__init__.py

+71-19
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,53 @@
3535
)
3636

3737

38-
class TimeoutError(RuntimeError):
38+
class AGPTimeoutError(TimeoutError):
3939
"""
40-
Custom exception class for timeout errors.
40+
Exception raised for AGP timeout errors.
41+
42+
This exception is raised when an operation in an AGP session times out.
43+
It encapsulates detailed information about the timeout event, including the
44+
ID of the message that caused the timeout and the session identifier. An
45+
optional underlying exception can also be provided to offer additional context.
46+
47+
Attributes:
48+
message_id (int): The identifier associated with the message triggering the timeout.
49+
session_id (int): The identifier of the session where the timeout occurred.
50+
message (str): A brief description of the timeout error.
51+
original_exception (Exception, optional): The underlying exception that caused the timeout, if any.
52+
53+
The string representation of the exception (via __str__) returns a full message that
54+
includes the custom message, session ID, and message ID, as well as details of the
55+
original exception (if present). This provides a richer context when the exception is logged
56+
or printed.
4157
"""
4258

43-
def __init__(self, message_id: int, session_id: int):
44-
self.message = f"Timeout error: message={message_id} session={session_id}"
45-
super().__init__(self.message)
59+
def __init__(
60+
self,
61+
message_id: int,
62+
session_id: int,
63+
message: str = "AGP timeout error",
64+
original_exception: Optional[Exception] = None,
65+
):
66+
67+
self.message_id = message_id
68+
self.session_id = session_id
69+
self.message = message
70+
self.original_exception = original_exception
71+
full_message = f"{message} for session {session_id} and message {message_id}"
72+
if original_exception:
73+
full_message = f"{full_message}. Caused by: {original_exception!r}"
74+
super().__init__(full_message)
75+
76+
def __str__(self):
77+
return self.args[0]
78+
79+
def __repr__(self):
80+
return (
81+
f"{self.__class__.__name__}(session_id={self.session_id!r}, "
82+
f"message_id={self.message_id!r}, "
83+
f"message={self.message!r}, original_exception={self.original_exception!r})"
84+
)
4685

4786

4887
class Gateway:
@@ -170,16 +209,21 @@ async def create_ff_session(
170209
queue_size: int = 0,
171210
) -> PySessionInfo:
172211
"""
173-
Create a new session.
212+
Create a new Fire-and-Forget session.
213+
This asynchronous function initializes a new session using the provided
214+
configuration and creates an associated message queue with the specified size.
215+
A session is created and stored along with its unbounded or bounded queue.
174216
175217
Args:
176-
session_config (PyFireAndForgetConfiguration): The session configuration.
177-
queue_size (int): The size of the queue for the session.
178-
If 0, the queue will be unbounded.
179-
If a positive integer, the queue will be bounded to that size.
218+
session_config (PyFireAndForgetConfiguration, optional): The configuration parameters for the session.
219+
Defaults to an instance of PyFireAndForgetConfiguration.
220+
queue_size (int, optional): The maximum size of the session's queue.
221+
If 0, the queue is unbounded.
222+
If a positive integer, the queue is bounded to that size.
223+
Defaults to 0.
180224
181225
Returns:
182-
ID of the session
226+
PySessionInfo: An object containing details about the created session.
183227
"""
184228

185229
session = await create_ff_session(self.svc, session_config)
@@ -192,16 +236,21 @@ async def create_rr_session(
192236
queue_size: int = 0,
193237
) -> PySessionInfo:
194238
"""
195-
Create a new session.
239+
Create a new Request-Response session.
240+
This asynchronous function initializes a new session using the provided
241+
configuration and creates an associated message queue with the specified size.
242+
A session is created and stored along with its unbounded or bounded queue.
196243
197244
Args:
198-
session_config (PyRequestResponseConfiguration): The session configuration.
199-
queue_size (int): The size of the queue for the session.
200-
If 0, the queue will be unbounded.
201-
If a positive integer, the queue will be bounded to that size.
245+
session_config (PyRequestResponseConfiguration, optional): The configuration parameters for the session.
246+
Defaults to an instance of PyRequestResponseConfiguration.
247+
queue_size (int, optional): The maximum size of the session's queue.
248+
If 0, the queue is unbounded.
249+
If a positive integer, the queue is bounded to that size.
250+
Defaults to 0.
202251
203252
Returns:
204-
ID of the session
253+
PySessionInfo: An object containing details about the created session.
205254
"""
206255

207256
session = await create_rr_session(self.svc, session_config)
@@ -239,11 +288,14 @@ async def delete_session(self, session_id: int):
239288
240289
Returns:
241290
None
291+
292+
Raises:
293+
ValueError: If the session ID is not found.
242294
"""
243295

244296
# Check if the session ID is in the sessions map
245297
if session_id not in self.sessions:
246-
raise Exception("session not found", session_id)
298+
raise ValueError(f"session not found: {session_id}")
247299

248300
# Remove the session from the map
249301
del self.sessions[session_id]
@@ -560,7 +612,7 @@ async def _receive_loop(self) -> None:
560612

561613
# figure out what exception to raise based on the reason
562614
if reason == "timeout":
563-
err = TimeoutError(message_id, session_id)
615+
err = AGPTimeoutError(message_id, session_id)
564616
else:
565617
# we don't know the reason, just raise the original exception
566618
raise e

0 commit comments

Comments
 (0)