Skip to content

Commit 5203ddf

Browse files
committed
Simplify socket error handling.
1 parent b0d7809 commit 5203ddf

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

packages/common/src/client/sync/stream/AbstractRemote.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,12 @@ export abstract class AbstractRemote {
312312
// automatically as a header.
313313
const userAgent = this.getUserAgent();
314314

315-
let socketCreationError: Error | undefined;
316-
317315
const url = this.options.socketUrlTransformer(request.url);
318316
const connector = new RSocketConnector({
319317
transport: new WebsocketClientTransport({
320318
url,
321319
wsCreator: (url) => {
322-
const s = this.createSocket(url);
323-
s.addEventListener('error', (e: ErrorEvent) => {
324-
socketCreationError = e.error ?? new Error(`Failed to create connection to websocket: ${url}`);
325-
this.logger.warn('Socket error', socketCreationError);
326-
});
327-
return s;
320+
return this.createSocket(url);
328321
}
329322
}),
330323
setup: {
@@ -348,11 +341,8 @@ export abstract class AbstractRemote {
348341
try {
349342
rsocket = await connector.connect();
350343
} catch (ex) {
351-
/**
352-
* On React native the connection exception can be `undefined` this causes issues
353-
* with detecting the exception inside async-mutex
354-
*/
355-
throw ex ?? socketCreationError;
344+
this.logger.error(`Failed to connect WebSocket`, ex);
345+
throw ex;
356346
}
357347

358348
const stream = new DataStream({

packages/common/src/client/sync/stream/WebsocketClientTransport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export class WebsocketClientTransport implements ClientTransport {
4444

4545
const errorListener = (ev: ErrorEvent) => {
4646
removeListeners();
47-
reject(ev.error);
47+
// ev.error is often undefined, especially on React Native and Web.
48+
// We add a default error in that case.
49+
const error = ev.error ?? new Error(`Failed to create connection to websocket: ${this.url}`);
50+
reject(error);
4851
};
4952

5053
/**

0 commit comments

Comments
 (0)