Skip to content

Commit 8255e0c

Browse files
SkyZeroZxthePunderWoman
authored andcommitted
feat(service-worker): add messageerror event handling and logging (#62834)
Add messageerror event handling and logging to Driver class PR Close #62834
1 parent b5fc949 commit 8255e0c

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

packages/service-worker/worker/src/driver.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ export class Driver implements Debuggable, UpdateSource {
189189
}
190190
});
191191

192-
// Handle the fetch, message, and push, notificationclick,
193-
// notificationclose and pushsubscriptionchange events.
192+
// Handle the fetch, message, push, notificationclick,
193+
// notificationclose, pushsubscriptionchange, and messageerror events.
194194
this.scope.addEventListener('fetch', (event) => this.onFetch(event!));
195195
this.scope.addEventListener('message', (event) => this.onMessage(event!));
196196
this.scope.addEventListener('push', (event) => this.onPush(event!));
@@ -201,6 +201,7 @@ export class Driver implements Debuggable, UpdateSource {
201201
// based on the incorrect assumption that browsers don't support it.
202202
this.onPushSubscriptionChange(event as PushSubscriptionChangeEvent),
203203
);
204+
this.scope.addEventListener('messageerror', (event) => this.onMessageError(event));
204205

205206
// The debugger generates debug pages in response to debugging requests.
206207
this.debugger = new DebugHandler(this, this.adapter);
@@ -338,6 +339,15 @@ export class Driver implements Debuggable, UpdateSource {
338339
event.waitUntil(this.handlePushSubscriptionChange(event));
339340
}
340341

342+
private onMessageError(event: MessageEvent): void {
343+
// Handle message deserialization errors that occur when receiving messages
344+
// that cannot be deserialized, typically due to corrupted data or unsupported formats.
345+
this.debugger.log(
346+
`Message error occurred - data could not be deserialized`,
347+
`Driver.onMessageError(origin: ${event.origin})`,
348+
);
349+
}
350+
341351
private async ensureInitialized(event: ExtendableEvent): Promise<void> {
342352
// Since the SW may have just been started, it may or may not have been initialized already.
343353
// `this.initialized` will be `null` if initialization has not yet been attempted, or will be a

packages/service-worker/worker/test/happy_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,21 @@ import {envIsSupported} from '../testing/utils';
11931193
});
11941194
});
11951195

1196+
describe('messageerror events', () => {
1197+
it('logs message deserialization errors', async () => {
1198+
await driver.initialized;
1199+
1200+
const debuggerLogSpy = spyOn(driver.debugger, 'log');
1201+
1202+
scope.handleMessageError('someClient');
1203+
1204+
expect(debuggerLogSpy).toHaveBeenCalledWith(
1205+
'Message error occurred - data could not be deserialized',
1206+
'Driver.onMessageError(origin: )',
1207+
);
1208+
});
1209+
});
1210+
11961211
describe('notification close events', () => {
11971212
it('broadcasts notification close events', async () => {
11981213
expect(await makeRequest(scope, '/foo.txt')).toEqual('this is foo');

packages/service-worker/worker/testing/scope.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ export class SwTestHarnessImpl
236236
return event.ready;
237237
}
238238

239+
handleMessageError(clientId: string | null) {
240+
if (!this.eventHandlers.has('messageerror')) {
241+
throw new Error('No messageerror handler registered');
242+
}
243+
244+
if (clientId && !this.clients.getMock(clientId)) {
245+
this.clients.add(clientId, this.scopeUrl);
246+
}
247+
248+
const event = new MockExtendableMessageEvent(
249+
null,
250+
(clientId && this.clients.getMock(clientId)) || null,
251+
);
252+
this.eventHandlers.get('messageerror')!.call(this, event);
253+
254+
return event.ready;
255+
}
256+
239257
handlePush(data: Object): Promise<void> {
240258
if (!this.eventHandlers.has('push')) {
241259
throw new Error('No push handler registered');

0 commit comments

Comments
 (0)