@@ -267,6 +267,9 @@ def _target_closed_future(self) -> asyncio.Future:
267
267
return asyncio .Future ()
268
268
return page ._closed_or_crashed_future
269
269
270
+ def _safe_page (self ) -> "Optional[Page]" :
271
+ return cast ("Frame" , from_channel (self ._initializer ["frame" ]))._page
272
+
270
273
271
274
class Route (ChannelOwner ):
272
275
def __init__ (
@@ -275,6 +278,7 @@ def __init__(
275
278
super ().__init__ (parent , type , guid , initializer )
276
279
self ._handling_future : Optional [asyncio .Future ["bool" ]] = None
277
280
self ._context : "BrowserContext" = cast ("BrowserContext" , None )
281
+ self ._did_throw = False
278
282
279
283
def _start_handling (self ) -> "asyncio.Future[bool]" :
280
284
self ._handling_future = asyncio .Future ()
@@ -298,17 +302,17 @@ def request(self) -> Request:
298
302
return from_channel (self ._initializer ["request" ])
299
303
300
304
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
+ )
309
314
)
310
315
)
311
- self ._report_handled (True )
312
316
313
317
async def fulfill (
314
318
self ,
@@ -320,7 +324,22 @@ async def fulfill(
320
324
contentType : str = None ,
321
325
response : "APIResponse" = None ,
322
326
) -> 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 :
324
343
params = locals_to_params (locals ())
325
344
326
345
if json is not None :
@@ -375,7 +394,15 @@ async def fulfill(
375
394
params ["requestUrl" ] = self .request ._initializer ["url" ]
376
395
377
396
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
379
406
380
407
async def fetch (
381
408
self ,
@@ -418,10 +445,12 @@ async def continue_(
418
445
postData : Union [Any , str , bytes ] = None ,
419
446
) -> None :
420
447
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 )
425
454
426
455
def _internal_continue (
427
456
self , is_internal : bool = False
@@ -458,11 +487,11 @@ async def continue_route() -> None:
458
487
return continue_route ()
459
488
460
489
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
+ )
464
494
)
465
- self ._report_handled (True )
466
495
467
496
async def _race_with_page_close (self , future : Coroutine ) -> None :
468
497
fut = asyncio .create_task (future )
0 commit comments