@@ -267,6 +267,9 @@ def _target_closed_future(self) -> asyncio.Future:
267267 return asyncio .Future ()
268268 return page ._closed_or_crashed_future
269269
270+ def _safe_page (self ) -> "Optional[Page]" :
271+ return cast ("Frame" , from_channel (self ._initializer ["frame" ]))._page
272+
270273
271274class Route (ChannelOwner ):
272275 def __init__ (
@@ -275,6 +278,7 @@ def __init__(
275278 super ().__init__ (parent , type , guid , initializer )
276279 self ._handling_future : Optional [asyncio .Future ["bool" ]] = None
277280 self ._context : "BrowserContext" = cast ("BrowserContext" , None )
281+ self ._did_throw = False
278282
279283 def _start_handling (self ) -> "asyncio.Future[bool]" :
280284 self ._handling_future = asyncio .Future ()
@@ -298,17 +302,17 @@ def request(self) -> Request:
298302 return from_channel (self ._initializer ["request" ])
299303
300304 async def abort (self , errorCode : str = None ) -> None :
301- self ._check_not_handled ()
302- await self ._race_with_page_close (
303- self ._channel .send (
304- "abort" ,
305- {
306- "errorCode" : errorCode ,
307- "requestUrl" : self .request ._initializer ["url" ],
308- },
305+ await self ._handle_route (
306+ lambda : self ._race_with_page_close (
307+ self ._channel .send (
308+ "abort" ,
309+ {
310+ "errorCode" : errorCode ,
311+ "requestUrl" : self .request ._initializer ["url" ],
312+ },
313+ )
309314 )
310315 )
311- self ._report_handled (True )
312316
313317 async def fulfill (
314318 self ,
@@ -320,7 +324,22 @@ async def fulfill(
320324 contentType : str = None ,
321325 response : "APIResponse" = None ,
322326 ) -> None :
323- self ._check_not_handled ()
327+ await self ._handle_route (
328+ lambda : self ._inner_fulfill (
329+ status , headers , body , json , path , contentType , response
330+ )
331+ )
332+
333+ async def _inner_fulfill (
334+ self ,
335+ status : int = None ,
336+ headers : Dict [str , str ] = None ,
337+ body : Union [str , bytes ] = None ,
338+ json : Any = None ,
339+ path : Union [str , Path ] = None ,
340+ contentType : str = None ,
341+ response : "APIResponse" = None ,
342+ ) -> None :
324343 params = locals_to_params (locals ())
325344
326345 if json is not None :
@@ -375,7 +394,15 @@ async def fulfill(
375394 params ["requestUrl" ] = self .request ._initializer ["url" ]
376395
377396 await self ._race_with_page_close (self ._channel .send ("fulfill" , params ))
378- self ._report_handled (True )
397+
398+ async def _handle_route (self , callback : Callable ) -> None :
399+ self ._check_not_handled ()
400+ try :
401+ await callback ()
402+ self ._report_handled (True )
403+ except Exception as e :
404+ self ._did_throw = True
405+ raise e
379406
380407 async def fetch (
381408 self ,
@@ -418,10 +445,12 @@ async def continue_(
418445 postData : Union [Any , str , bytes ] = None ,
419446 ) -> None :
420447 overrides = cast (FallbackOverrideParameters , locals_to_params (locals ()))
421- self ._check_not_handled ()
422- self .request ._apply_fallback_overrides (overrides )
423- await self ._internal_continue ()
424- self ._report_handled (True )
448+
449+ async def _inner () -> None :
450+ self .request ._apply_fallback_overrides (overrides )
451+ await self ._internal_continue ()
452+
453+ return await self ._handle_route (_inner )
425454
426455 def _internal_continue (
427456 self , is_internal : bool = False
@@ -458,11 +487,11 @@ async def continue_route() -> None:
458487 return continue_route ()
459488
460489 async def _redirected_navigation_request (self , url : str ) -> None :
461- self ._check_not_handled ()
462- await self ._race_with_page_close (
463- self ._channel .send ("redirectNavigationRequest" , {"url" : url })
490+ await self ._handle_route (
491+ lambda : self ._race_with_page_close (
492+ self ._channel .send ("redirectNavigationRequest" , {"url" : url })
493+ )
464494 )
465- self ._report_handled (True )
466495
467496 async def _race_with_page_close (self , future : Coroutine ) -> None :
468497 fut = asyncio .create_task (future )
0 commit comments