Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit588dbd3

Browse files
chrisguttandinmhevery
authored andcommitted
fix(service-worker): handle error with ErrorHandler (#39990)
Errors thrown by calling serviceWorker.register() are now passed to the global ErrorHandler.Fixes#39913PRClose#39990
1 parent97310d3 commit588dbd3

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

‎packages/service-worker/src/module.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import{isPlatformBrowser}from'@angular/common';
10-
import{APP_INITIALIZER,ApplicationRef,InjectionToken,Injector,ModuleWithProviders,NgModule,NgZone,PLATFORM_ID}from'@angular/core';
10+
import{APP_INITIALIZER,ApplicationRef,ErrorHandler,InjectionToken,Injector,ModuleWithProviders,NgModule,NgZone,PLATFORM_ID}from'@angular/core';
1111
import{merge,Observable,of}from'rxjs';
1212
import{delay,filter,take}from'rxjs/operators';
1313

@@ -128,9 +128,10 @@ export function ngswAppInitializer(
128128
constngZone=injector.get(NgZone);
129129
ngZone.runOutsideAngular(
130130
()=>readyToRegister$.pipe(take(1)).subscribe(
131-
()=>
132-
navigator.serviceWorker.register(script,{scope:options.scope})
133-
.catch(err=>console.error('Service worker registration failed with:',err))));
131+
()=>navigator.serviceWorker.register(script,{scope:options.scope}).catch(err=>{
132+
consterrorHandler=injector.get(ErrorHandler);
133+
errorHandler.handleError(err);
134+
})));
134135
};
135136
returninitializer;
136137
}

‎packages/service-worker/test/module_spec.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import{ApplicationRef,PLATFORM_ID}from'@angular/core';
9+
import{ApplicationRef,ErrorHandler,PLATFORM_ID}from'@angular/core';
1010
import{fakeAsync,flushMicrotasks,TestBed,tick}from'@angular/core/testing';
1111
import{Subject}from'rxjs';
1212
import{filter,take}from'rxjs/operators';
@@ -21,6 +21,7 @@ describe('ServiceWorkerModule', () => {
2121
return;
2222
}
2323

24+
leterrorHandlerSpy:jasmine.Spy;
2425
letswRegisterSpy:jasmine.Spy;
2526

2627
constuntilStable=()=>{
@@ -34,9 +35,14 @@ describe('ServiceWorkerModule', () => {
3435

3536
describe('register()',()=>{
3637
constconfigTestBed=async(opts:SwRegistrationOptions)=>{
38+
consterrorHandler={handleError:()=>{}};
39+
errorHandlerSpy=spyOn(errorHandler,'handleError');
3740
TestBed.configureTestingModule({
3841
imports:[ServiceWorkerModule.register('sw.js',opts)],
39-
providers:[{provide:PLATFORM_ID,useValue:'browser'}],
42+
providers:[
43+
{provide:ErrorHandler,useValue:errorHandler},
44+
{provide:PLATFORM_ID,useValue:'browser'},
45+
],
4046
});
4147

4248
awaituntilStable();
@@ -71,12 +77,10 @@ describe('ServiceWorkerModule', () => {
7177
});
7278

7379
it('catches and a logs registration errors',async()=>{
74-
constconsoleErrorSpy=spyOn(console,'error');
7580
swRegisterSpy.and.returnValue(Promise.reject('no reason'));
7681

7782
awaitconfigTestBed({enabled:true,scope:'foo'});
78-
expect(consoleErrorSpy)
79-
.toHaveBeenCalledWith('Service worker registration failed with:','no reason');
83+
expect(errorHandlerSpy).toHaveBeenCalledWith('no reason');
8084
});
8185
});
8286

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp