Skip to content

Commit e91d7a8

Browse files
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 <dagmar.hoogendorp@wearespindle.com> Co-authored-by: Dag <dagmar.hoogendorp@wearespindle.com>
1 parent 52129ec commit e91d7a8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/transport.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,24 +357,28 @@ export class ReconnectableTransport extends EventEmitter implements ITransport {
357357
}
358358

359359
private isOnlinePromise(mode: ReconnectionMode) {
360-
return new Promise(resolve => {
360+
return new Promise((resolve, reject) => {
361361
const checkSocket = new WebSocket(this.uaOptions.transportOptions.wsServers, 'sip');
362362

363363
const handlers = {
364364
onError: e => {
365365
log.debug(e, this.constructor.name);
366+
366367
checkSocket.removeEventListener('open', handlers.onOpen);
367-
// In the case that mode is BURST, throw an error which can be
368-
// catched by pRetry.
368+
checkSocket.removeEventListener('error', handlers.onError);
369+
370+
// In the case that mode is BURST, reject the promise which can be
371+
// caught by pRetry.
369372
if (mode === ReconnectionMode.BURST) {
370-
throw new Error('it broke woops');
373+
reject('Connection to sip server broke, try to reconnect');
374+
} else {
375+
resolve(false);
371376
}
372-
373-
resolve(false);
374377
},
375378
onOpen: () => {
376379
log.debug('Opening a socket to sip server worked.', this.constructor.name);
377380
checkSocket.close();
381+
checkSocket.removeEventListener('open', handlers.onOpen);
378382
checkSocket.removeEventListener('error', handlers.onError);
379383
resolve(true);
380384
}
@@ -432,13 +436,13 @@ export class ReconnectableTransport extends EventEmitter implements ITransport {
432436
// https://tools.ietf.org/html/rfc6026#section-7.1
433437

434438
// As noted in the comment above, we are to leaving it to the transaction
435-
// timers to evenutally cause the transaction to sort itself out in the case
439+
// timers to eventually cause the transaction to sort itself out in the case
436440
// of a transport failure in an invite server transaction. This delegate method
437441
// is here simply here for completeness and to make it clear that it provides
438442
// nothing more than informational hook into the core. That is, if you think
439443
// you should be trying to deal with a transport error here, you are likely wrong.
440444
log.error(
441-
'A transport error has occured while handling an incoming INVITE request.',
445+
'A transport error has occurred while handling an incoming INVITE request.',
442446
this.constructor.name
443447
);
444448
}
@@ -550,7 +554,7 @@ export class ReconnectableTransport extends EventEmitter implements ITransport {
550554
const tryOpeningSocketWithTimeout = () =>
551555
pTimeout(this.isOnlinePromise(mode), 5000, () => {
552556
// In the case that mode is BURST, throw an error which can be
553-
// catched by pRetry.
557+
// caught by pRetry.
554558
if (mode === ReconnectionMode.BURST) {
555559
throw new Error('Cannot open socket. Probably DNS failure.');
556560
}

0 commit comments

Comments
 (0)