Skip to content

fix(service-worker): handle error with ErrorHandler #39990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(service-worker): handle error with ErrorHandler
Errors thrown by calling serviceWorker.register() are now passed to the global ErrorHandler.

Fixes #39913
  • Loading branch information
chrisguttandin authored and mhevery committed Dec 8, 2020
commit 552419d0aaaef0aaaa04853226e100a7df57fdcf
9 changes: 5 additions & 4 deletions packages/service-worker/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

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

Expand Down Expand Up @@ -128,9 +128,10 @@ export function ngswAppInitializer(
const ngZone = injector.get(NgZone);
ngZone.runOutsideAngular(
() => readyToRegister$.pipe(take(1)).subscribe(
() =>
navigator.serviceWorker.register(script, {scope: options.scope})
.catch(err => console.error('Service worker registration failed with:', err))));
() => navigator.serviceWorker.register(script, {scope: options.scope}).catch(err => {
const errorHandler = injector.get(ErrorHandler);
errorHandler.handleError(err);
})));
};
return initializer;
}
Expand Down
14 changes: 9 additions & 5 deletions packages/service-worker/test/module_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

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

let errorHandlerSpy: jasmine.Spy;
let swRegisterSpy: jasmine.Spy;

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

describe('register()', () => {
const configTestBed = async (opts: SwRegistrationOptions) => {
const errorHandler = {handleError: () => {}};
errorHandlerSpy = spyOn(errorHandler, 'handleError');
TestBed.configureTestingModule({
imports: [ServiceWorkerModule.register('sw.js', opts)],
providers: [{provide: PLATFORM_ID, useValue: 'browser'}],
providers: [
{provide: ErrorHandler, useValue: errorHandler},
{provide: PLATFORM_ID, useValue: 'browser'},
],
});

await untilStable();
Expand Down Expand Up @@ -71,12 +77,10 @@ describe('ServiceWorkerModule', () => {
});

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

await configTestBed({enabled: true, scope: 'foo'});
expect(consoleErrorSpy)
.toHaveBeenCalledWith('Service worker registration failed with:', 'no reason');
expect(errorHandlerSpy).toHaveBeenCalledWith('no reason');
});
});

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy