8000 fix(service-worker): handle error with ErrorHandler (#39990) · angular/angular@588dbd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 588dbd3

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 #39913 PR Close #39990
1 parent 97310d3 commit 588dbd3

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

packages/service-worker/src/module.ts

+5-4
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
const ngZone = 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+
const errorHandler = injector.get(ErrorHandler);
133+
errorHandler.handleError(err);
134+
})));
134135
};
135136
return initializer;
136137
}

packages/service-worker/test/module_spec.ts

+9-5
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+
let errorHandlerSpy: jasmine.Spy;
2425
let swRegisterSpy: jasmine.Spy;
2526

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

3536
describe('register()', () => {
3637
const configTestBed = async (opts: SwRegistrationOptions) => {
38+
const errorHandler = {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
await untilStable();
@@ -71,12 +77,10 @@ describe('ServiceWorkerModule', () => {
7177
});
7278

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

7782
await configTestBed({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)
0