@@ -334,10 +334,14 @@ export abstract class BrowserContext extends SdkObject {
334
334
const binding = new PageBinding ( name , playwrightBinding , needsHandle ) ;
335
335
binding . forClient = forClient ;
336
336
this . _pageBindings . set ( name , binding ) ;
337
- progress . cleanupWhenAborted ( ( ) => this . _pageBindings . delete ( name ) ) ;
338
- await progress . race ( this . doAddInitScript ( binding . initScript ) ) ;
339
- await progress . race ( this . safeNonStallingEvaluateInAllFrames ( binding . initScript . source , 'main' ) ) ;
340
- return binding ;
337
+ try {
338
+ await progress . race ( this . doAddInitScript ( binding . initScript ) ) ;
339
+ await progress . race ( this . safeNonStallingEvaluateInAllFrames ( binding . initScript . source , 'main' ) ) ;
340
+ return binding ;
341
+ } catch ( error ) {
342
+ this . _pageBindings . delete ( name ) ;
343
+ throw error ;
344
+ }
341
345
}
342
346
343
347
async removeExposedBindings ( bindings : PageBinding [ ] ) {
@@ -370,21 +374,27 @@ export abstract class BrowserContext extends SdkObject {
370
374
async setExtraHTTPHeaders ( progress : Progress , headers : types . HeadersArray ) {
371
375
const oldHeaders = this . _options . extraHTTPHeaders ;
372
376
this . _options . extraHTTPHeaders = headers ;
373
- progress . cleanupWhenAborted ( async ( ) => {
377
+ try {
378
+ await progress . race ( this . doUpdateExtraHTTPHeaders ( ) ) ;
379
+ } catch ( error ) {
374
380
this . _options . extraHTTPHeaders = oldHeaders ;
375
- await this . doUpdateExtraHTTPHeaders ( ) ;
376
- } ) ;
377
- await progress . race ( this . doUpdateExtraHTTPHeaders ( ) ) ;
381
+ // Note: no await, headers will be reset in the background as soon as possible.
382
+ this . doUpdateExtraHTTPHeaders ( ) . catch ( ( ) => { } ) ;
383
+ throw error ;
384
+ }
378
385
}
379
386
380
387
async setOffline ( progress : Progress , offline : boolean ) {
381
388
const oldOffline = this . _options . offline ;
382
389
this . _options . offline = offline ;
383
- progress . cleanupWhenAborted ( async ( ) => {
390
+ try {
391
+ await progress . race ( this . doUpdateOffline ( ) ) ;
392
+ } catch ( error ) {
384
393
this . _options . offline = oldOffline ;
385
- await this . doUpdateOffline ( ) ;
386
- } ) ;
387
- await progress . race ( this . doUpdateOffline ( ) ) ;
394
+ // Note: no await, offline will be reset in the background as soon as possible.
395
+ this . doUpdateOffline ( ) . catch ( ( ) => { } ) ;
396
+ throw error ;
397
+ }
388
398
}
389
399
390
400
async _loadDefaultContextAsIs ( progress : Progress ) : Promise < Page | undefined > {
@@ -442,13 +452,18 @@ export abstract class BrowserContext extends SdkObject {
442
452
async addInitScript ( progress : Progress | undefined , source : string ) {
443
453
const initScript = new InitScript ( source ) ;
444
454
this . initScripts . push ( initScript ) ;
445
- progress ?. cleanupWhenAborted ( ( ) => this . removeInitScripts ( [ initScript ] ) ) ;
446
- const promise = this . doAddInitScript ( initScript ) ;
447
- if ( progress )
448
- await progress . race ( promise ) ;
449
- else
450
- await promise ;
451
- return initScript ;
455
+ try {
456
+ const promise = this . doAddInitScript ( initScript ) ;
457
+ if ( progress )
458
+ await progress . race ( promise ) ;
459
+ else
460
+ await promise ;
461
+ return initScript ;
462
+ } catch ( error ) {
463
+ // Note: no await, init script will be removed in the background as soon as possible.
464
+ this . removeInitScripts ( [ initScript ] ) . catch ( ( ) => { } ) ;
465
+ throw error ;
466
+ }
452
467
}
453
468
454
469
async removeInitScripts ( initScripts : InitScript [ ] ) {
@@ -528,9 +543,8 @@ export abstract class BrowserContext extends SdkObject {
528
543
}
529
544
530
545
async newPage ( progress : Progress , isServerSide : boolean ) : Promise < Page > {
531
- let page : Page | undefined ;
546
+ const page = await progress . race ( this . doCreateNewPage ( isServerSide ) ) ;
532
547
try {
533
- page = await progress . race ( this . doCreateNewPage ( isServerSide ) ) ;
534
548
const pageOrError = await progress . race ( page . waitForInitializedOrError ( ) ) ;
535
549
if ( pageOrError instanceof Page ) {
536
550
if ( pageOrError . isClosed ( ) )
@@ -539,7 +553,7 @@ export abstract class BrowserContext extends SdkObject {
539
553
}
540
554
throw pageOrError ;
541
555
} catch ( error ) {
542
- await page ? .close ( { reason : 'Failed to create page' } ) . catch ( ( ) => { } ) ;
556
+ await page . close ( { reason : 'Failed to create page' } ) . catch ( ( ) => { } ) ;
543
557
throw error ;
544
558
}
545
559
}
@@ -580,17 +594,20 @@ export abstract class BrowserContext extends SdkObject {
580
594
// If there are still origins to save, create a blank page to iterate over origins.
581
595
if ( originsToSave . size ) {
582
596
const page = await this . newPage ( progress , true ) ;
583
- await page . addRequestInterceptor ( progress , route => {
584
- route . fulfill ( { body : '<html></html>' } ) . catch ( ( ) => { } ) ;
585
- } , 'prepend' ) ;
586
- for ( const origin of originsToSave ) {
587
- const frame = page . mainFrame ( ) ;
588
- await frame . gotoImpl ( progress , origin , { } ) ;
589
- const storage : SerializedStorage = await progress . race ( frame . evaluateExpression ( collectScript , { world : 'utility' } ) ) ;
590
- if ( storage . localStorage . length || storage . indexedDB ?. length )
591
- result . origins . push ( { origin, localStorage : storage . localStorage , indexedDB : storage . indexedDB } ) ;
597
+ try {
598
+ await page . addRequestInterceptor ( progress , route => {
599
+ route . fulfill ( { body : '<html></html>' } ) . catch ( ( ) => { } ) ;
600
+ } , 'prepend' ) ;
601
+ for ( const origin of originsToSave ) {
602
+ const frame = page . mainFrame ( ) ;
603
+ await frame . gotoImpl ( progress , origin , { } ) ;
604
+ const storage : SerializedStorage = await progress . race ( frame . evaluateExpression ( collectScript , { world : 'utility' } ) ) ;
605
+ if ( storage . localStorage . length || storage . indexedDB ?. length )
606
+ result . origins . push ( { origin, localStorage : storage . localStorage , indexedDB : storage . indexedDB } ) ;
607
+ }
608
+ } finally {
609
+ await page . close ( ) ;
592
610
}
593
- await page . close ( ) ;
594
611
}
595
612
return result ;
596
613
}
@@ -608,33 +625,34 @@ export abstract class BrowserContext extends SdkObject {
608
625
const interceptor = ( route : network . Route ) => {
609
626
route . fulfill ( { body : '<html></html>' } ) . catch ( ( ) => { } ) ;
610
627
} ;
611
-
612
- progress . cleanupWhenAborted ( ( ) => page . removeRequestInterceptor ( interceptor ) ) ;
613
628
await page . addRequestInterceptor ( progress , interceptor , 'prepend' ) ;
614
629
615
- for ( const origin of new Set ( [ ...oldOrigins , ...newOrigins . keys ( ) ] ) ) {
616
- const frame = page . mainFrame ( ) ;
617
- await frame . gotoImpl ( progress , origin , { } ) ;
618
- await progress . race ( frame . resetStorageForCurrentOriginBestEffort ( newOrigins . get ( origin ) ) ) ;
619
- }
620
-
621
- await page . removeRequestInterceptor ( interceptor ) ;
630
+ try {
631
+ for ( const origin of new Set ( [ ...oldOrigins , ...newOrigins . keys ( ) ] ) ) {
632
+ const frame = page . mainFrame ( ) ;
633
+ await frame . gotoImpl ( progress , origin , { } ) ;
634
+ await progress . race ( frame . resetStorageForCurrentOriginBestEffort ( newOrigins . get ( origin ) ) ) ;
635
+ }
622
636
623
- this . _origins = new Set ( [ ...newOrigins . keys ( ) ] ) ;
624
- // It is safe to not restore the URL to about:blank since we are doing it in Page::resetForReuse.
637
+ this . _origins = new Set ( [ ...newOrigins . keys ( ) ] ) ;
638
+ // It is safe to not restore the URL to about:blank since we are doing it in Page::resetForReuse.
639
+ } finally {
640
+ await page . removeRequestInterceptor ( interceptor ) ;
641
+ }
625
642
}
626
643
627
644
isSettingStorageState ( ) : boolean {
628
645
return this . _settingStorageState ;
629
646
}
630
647
631
648
async setStorageState ( progress : Progress , state : NonNullable < channels . BrowserNewContextParams [ 'storageState' ] > ) {
649
+ let page : Page | undefined ;
632
650
this . _settingStorageState = true ;
633
651
try {
634
652
if ( state . cookies )
635
653
await progress . race ( this . addCookies ( state . cookies ) ) ;
636
654
if ( state . origins && state . origins . length ) {
637
- const page = await this . newPage ( progress , true ) ;
655
+ page = await this . newPage ( progress , true ) ;
638
656
await page . addRequestInterceptor ( progress , route => {
639
657
route . fulfill ( { body : '<html></html>' } ) . catch ( ( ) => { } ) ;
640
658
} , 'prepend' ) ;
@@ -649,12 +667,12 @@ export abstract class BrowserContext extends SdkObject {
649
667
})()` ;
650
668
await progress . race ( frame . evaluateExpression ( restoreScript , { world : 'utility' } ) ) ;
651
669
}
652
- await page . close ( ) ;
653
670
}
654
671
} catch ( error ) {
655
672
rewriteErrorMessage ( error , `Error setting storage state:\n` + error . message ) ;
656
673
throw error ;
657
674
} finally {
675
+ await page ?. close ( ) ;
658
676
this . _settingStorageState = false ;
659
677
}
660
678
}
0 commit comments