35
35
)
36
36
37
37
38
- class TimeoutError ( RuntimeError ):
38
+ class AGPTimeoutError ( TimeoutError ):
39
39
"""
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.
41
57
"""
42
58
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
+ )
46
85
47
86
48
87
class Gateway :
@@ -170,16 +209,21 @@ async def create_ff_session(
170
209
queue_size : int = 0 ,
171
210
) -> PySessionInfo :
172
211
"""
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.
174
216
175
217
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.
180
224
181
225
Returns:
182
- ID of the session
226
+ PySessionInfo: An object containing details about the created session.
183
227
"""
184
228
185
229
session = await create_ff_session (self .svc , session_config )
@@ -192,16 +236,21 @@ async def create_rr_session(
192
236
queue_size : int = 0 ,
193
237
) -> PySessionInfo :
194
238
"""
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.
196
243
197
244
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.
202
251
203
252
Returns:
204
- ID of the session
253
+ PySessionInfo: An object containing details about the created session.
205
254
"""
206
255
207
256
session = await create_rr_session (self .svc , session_config )
@@ -239,11 +288,14 @@ async def delete_session(self, session_id: int):
239
288
240
289
Returns:
241
290
None
291
+
292
+ Raises:
293
+ ValueError: If the session ID is not found.
242
294
"""
243
295
244
296
# Check if the session ID is in the sessions map
245
297
if session_id not in self .sessions :
246
- raise Exception ( "session not found" , session_id )
298
+ raise ValueError ( f "session not found: { session_id } " )
247
299
248
300
# Remove the session from the map
249
301
del self .sessions [session_id ]
@@ -560,7 +612,7 @@ async def _receive_loop(self) -> None:
560
612
561
613
# figure out what exception to raise based on the reason
562
614
if reason == "timeout" :
563
- err = TimeoutError (message_id , session_id )
615
+ err = AGPTimeoutError (message_id , session_id )
564
616
else :
565
617
# we don't know the reason, just raise the original exception
566
618
raise e
0 commit comments