chore: log when websockets are proactively closed (#23689)

Closes https://github.com/microsoft/playwright/issues/23566

n.b., while that issue describes a fairly specific "use case", this
logging is simple and generic. It seems very plausible that it can help
diagnose all sorts of issues.

Cheers - V
This commit is contained in:
vemv 2023-06-19 20:12:02 +02:00 committed by GitHub
parent 0f9f863183
commit 380209af37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -125,10 +125,20 @@ export class WebSocketTransport implements ConnectionTransport {
this._ws.addEventListener('message', event => { this._ws.addEventListener('message', event => {
messageWrap(() => { messageWrap(() => {
const eventData = event.data as string;
let parsedJson;
try {
parsedJson = JSON.parse(eventData);
} catch (e) {
this._progress?.log(`<closing ws> Closing websocket due to malformed JSON. eventData=${eventData} e=${e?.message}`);
this._ws.close();
return;
}
try { try {
if (this.onmessage) if (this.onmessage)
this.onmessage.call(null, JSON.parse(event.data as string)); this.onmessage.call(null, parsedJson);
} catch (e) { } catch (e) {
this._progress?.log(`<closing ws> Closing websocket due to failed onmessage callback. eventData=${eventData} e=${e?.message}`);
this._ws.close(); this._ws.close();
} }
}); });