GeezerVison is showing github.com — fix(service-worker): handle error with ErrorHandler (#39990) · angular/angular@588dbd3
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/service-worker/src/module.ts +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | */ | 8 | 8 |
| 9 | 9 | 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'; | 11 | 11 | import {merge, Observable, of} from 'rxjs'; | 12 | 12 | import {delay, filter, take} from 'rxjs/operators'; | 13 | 13 |
| @@ -128,9 +128,10 @@ export function ngswAppInitializer( | 128 | 128 | const ngZone = injector.get(NgZone); | 129 | 129 | ngZone.runOutsideAngular( | 130 | 130 | () => 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 | + }))); | 134 | 135 | }; | 135 | 136 | return initializer; | 136 | 137 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/service-worker/test/module_spec.ts +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | * found in the LICENSE file at https://angular.io/license | 7 | 7 | */ | 8 | 8 |
| 9 | | -import {ApplicationRef, PLATFORM_ID} from '@angular/core'; | | 9 | +import {ApplicationRef, ErrorHandler, PLATFORM_ID} from '@angular/core'; | 10 | 10 | import {fakeAsync, flushMicrotasks, TestBed, tick} from '@angular/core/testing'; | 11 | 11 | import {Subject} from 'rxjs'; | 12 | 12 | import {filter, take} from 'rxjs/operators'; | @@ -21,6 +21,7 @@ describe('ServiceWorkerModule', () => { | 21 | 21 | return; | 22 | 22 | } | 23 | 23 |
| | 24 | + let errorHandlerSpy: jasmine.Spy; | 24 | 25 | let swRegisterSpy: jasmine.Spy; | 25 | 26 |
| 26 | 27 | const untilStable = () => { | @@ -34,9 +35,14 @@ describe('ServiceWorkerModule', () => { | 34 | 35 |
| 35 | 36 | describe('register()', () => { | 36 | 37 | const configTestBed = async (opts: SwRegistrationOptions) => { | | 38 | + const errorHandler = {handleError: () => {}}; | | 39 | + errorHandlerSpy = spyOn(errorHandler, 'handleError'); | 37 | 40 | TestBed.configureTestingModule({ | 38 | 41 | 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 | + ], | 40 | 46 | }); | 41 | 47 |
| 42 | 48 | await untilStable(); | @@ -71,12 +77,10 @@ describe('ServiceWorkerModule', () => { | 71 | 77 | }); | 72 | 78 |
| 73 | 79 | it('catches and a logs registration errors', async () => { | 74 | | - const consoleErrorSpy = spyOn(console, 'error'); | 75 | 80 | swRegisterSpy.and.returnValue(Promise.reject('no reason')); | 76 | 81 |
| 77 | 82 | await configTestBed({enabled: true, scope: 'foo'}); | 78 | | - expect(consoleErrorSpy) | 79 | | - .toHaveBeenCalledWith('Service worker registration failed with:', 'no reason'); | | 83 | + expect(errorHandlerSpy).toHaveBeenCalledWith('no reason'); | 80 | 84 | }); | 81 | 85 | }); | 82 | 86 |
| |
|