From e91d7a83951597cc403c881dd088dae22ac9df33 Mon Sep 17 00:00:00 2001 From: patrickswijgman Date: Mon, 28 Nov 2022 09:26:21 +0100 Subject: [PATCH] Handle 'it broke woops' error (#120) * Removed 'it broke woops' error that is thrown in the reconnect handler but is not handled by another handler * Actually using reject now instead * Remove all event listeners whenever the websocket opens or has an error * If else instead of return * Update src/transport.ts Co-authored-by: Dag Co-authored-by: Dag --- src/transport.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/transport.ts b/src/transport.ts index 591ea3e..39ebe77 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -357,24 +357,28 @@ export class ReconnectableTransport extends EventEmitter implements ITransport { } private isOnlinePromise(mode: ReconnectionMode) { - return new Promise(resolve => { + return new Promise((resolve, reject) => { const checkSocket = new WebSocket(this.uaOptions.transportOptions.wsServers, 'sip'); const handlers = { onError: e => { log.debug(e, this.constructor.name); + checkSocket.removeEventListener('open', handlers.onOpen); - // In the case that mode is BURST, throw an error which can be - // catched by pRetry. + checkSocket.removeEventListener('error', handlers.onError); + + // In the case that mode is BURST, reject the promise which can be + // caught by pRetry. if (mode === ReconnectionMode.BURST) { - throw new Error('it broke woops'); + reject('Connection to sip server broke, try to reconnect'); + } else { + resolve(false); } - - resolve(false); }, onOpen: () => { log.debug('Opening a socket to sip server worked.', this.constructor.name); checkSocket.close(); + checkSocket.removeEventListener('open', handlers.onOpen); checkSocket.removeEventListener('error', handlers.onError); resolve(true); } @@ -432,13 +436,13 @@ export class ReconnectableTransport extends EventEmitter implements ITransport { // https://tools.ietf.org/html/rfc6026#section-7.1 // As noted in the comment above, we are to leaving it to the transaction - // timers to evenutally cause the transaction to sort itself out in the case + // timers to eventually cause the transaction to sort itself out in the case // of a transport failure in an invite server transaction. This delegate method // is here simply here for completeness and to make it clear that it provides // nothing more than informational hook into the core. That is, if you think // you should be trying to deal with a transport error here, you are likely wrong. log.error( - 'A transport error has occured while handling an incoming INVITE request.', + 'A transport error has occurred while handling an incoming INVITE request.', this.constructor.name ); } @@ -550,7 +554,7 @@ export class ReconnectableTransport extends EventEmitter implements ITransport { const tryOpeningSocketWithTimeout = () => pTimeout(this.isOnlinePromise(mode), 5000, () => { // In the case that mode is BURST, throw an error which can be - // catched by pRetry. + // caught by pRetry. if (mode === ReconnectionMode.BURST) { throw new Error('Cannot open socket. Probably DNS failure.'); }