@@ -62,6 +62,38 @@ def _validate_max_wait_seconds(max_wait_seconds: Optional[float]) -> None:
6262 raise HyperbrowserError ("max_wait_seconds must be non-negative" )
6363
6464
65+ def _validate_page_batch_values (
66+ * ,
67+ operation_name : str ,
68+ current_page_batch : int ,
69+ total_page_batches : int ,
70+ ) -> None :
71+ if isinstance (current_page_batch , bool ) or not isinstance (current_page_batch , int ):
72+ raise HyperbrowserPollingError (
73+ f"Invalid current page batch for { operation_name } : expected integer"
74+ )
75+ if isinstance (total_page_batches , bool ) or not isinstance (total_page_batches , int ):
76+ raise HyperbrowserPollingError (
77+ f"Invalid total page batches for { operation_name } : expected integer"
78+ )
79+ if total_page_batches < 0 :
80+ raise HyperbrowserPollingError (
81+ f"Invalid total page batches for { operation_name } : must be non-negative"
82+ )
83+ if current_page_batch < 0 :
84+ raise HyperbrowserPollingError (
85+ f"Invalid current page batch for { operation_name } : must be non-negative"
86+ )
87+ if total_page_batches > 0 and current_page_batch < 1 :
88+ raise HyperbrowserPollingError (
89+ f"Invalid current page batch for { operation_name } : must be at least 1 when total batches are positive"
90+ )
91+ if current_page_batch > total_page_batches :
92+ raise HyperbrowserPollingError (
93+ f"Invalid page batch state for { operation_name } : current page batch { current_page_batch } exceeds total page batches { total_page_batches } "
94+ )
95+
96+
6597def has_exceeded_max_wait (start_time : float , max_wait_seconds : Optional [float ]) -> bool :
6698 return (
6799 max_wait_seconds is not None
@@ -241,6 +273,11 @@ def collect_paginated_results(
241273 on_page_success (page_response )
242274 current_page_batch = get_current_page_batch (page_response )
243275 total_page_batches = get_total_page_batches (page_response )
276+ _validate_page_batch_values (
277+ operation_name = operation_name ,
278+ current_page_batch = current_page_batch ,
279+ total_page_batches = total_page_batches ,
280+ )
244281 failures = 0
245282 first_check = False
246283 if (
@@ -303,6 +340,11 @@ async def collect_paginated_results_async(
303340 on_page_success (page_response )
304341 current_page_batch = get_current_page_batch (page_response )
305342 total_page_batches = get_total_page_batches (page_response )
343+ _validate_page_batch_values (
344+ operation_name = operation_name ,
345+ current_page_batch = current_page_batch ,
346+ total_page_batches = total_page_batches ,
347+ )
306348 failures = 0
307349 first_check = False
308350 if (
0 commit comments