@@ -45,7 +45,7 @@ import {
4545 protoInt64 ,
4646} from '@livekit/protocol' ;
4747import log , { LoggerNames , getLogger } from '../logger' ;
48- import { ConnectionError , ConnectionErrorReason } from '../room/errors' ;
48+ import { ConnectionError } from '../room/errors' ;
4949import CriticalTimers from '../room/timers' ;
5050import type { LoggerOptions } from '../room/types' ;
5151import { getClientInfo , isReactNative , sleep } from '../room/utils' ;
@@ -319,7 +319,7 @@ export class SignalClient {
319319 this . close ( ) ;
320320 }
321321 cleanupAbortHandlers ( ) ;
322- reject ( target instanceof AbortSignal ? target . reason : target ) ;
322+ reject ( ConnectionError . cancelled ( reason ) ) ;
323323 } ;
324324
325325 abortSignal ?. addEventListener ( 'abort' , abortHandler ) ;
@@ -330,12 +330,7 @@ export class SignalClient {
330330 } ;
331331
332332 const wsTimeout = setTimeout ( ( ) => {
333- abortHandler (
334- new ConnectionError (
335- 'room connection has timed out (signal)' ,
336- ConnectionErrorReason . ServerUnreachable ,
337- ) ,
338- ) ;
333+ abortHandler ( ConnectionError . timeout ( 'room connection has timed out (signal)' ) ) ;
339334 } , opts . websocketTimeout ) ;
340335
341336 const handleSignalConnected = (
@@ -364,9 +359,8 @@ export class SignalClient {
364359 . then ( ( closeInfo ) => {
365360 if ( this . isEstablishingConnection ) {
366361 reject (
367- new ConnectionError (
362+ ConnectionError . internal (
368363 `Websocket got closed during a (re)connection attempt: ${ closeInfo . reason } ` ,
369- ConnectionErrorReason . InternalError ,
370364 ) ,
371365 ) ;
372366 }
@@ -387,9 +381,8 @@ export class SignalClient {
387381 . catch ( ( reason ) => {
388382 if ( this . isEstablishingConnection ) {
389383 reject (
390- new ConnectionError (
384+ ConnectionError . internal (
391385 `Websocket error during a (re)connection attempt: ${ reason } ` ,
392- ConnectionErrorReason . InternalError ,
393386 ) ,
394387 ) ;
395388 }
@@ -416,10 +409,7 @@ export class SignalClient {
416409 const firstMessage = await signalReader . read ( ) ;
417410 signalReader . releaseLock ( ) ;
418411 if ( ! firstMessage . value ) {
419- throw new ConnectionError (
420- 'no message received as first message' ,
421- ConnectionErrorReason . InternalError ,
422- ) ;
412+ throw ConnectionError . internal ( 'no message received as first message' ) ;
423413 }
424414
425415 const firstSignalResponse = parseSignalResponse ( firstMessage . value ) ;
@@ -971,27 +961,24 @@ export class SignalClient {
971961 } else if ( this . isEstablishingConnection && firstSignalResponse . message ?. case === 'leave' ) {
972962 return {
973963 isValid : false ,
974- error : new ConnectionError (
964+ error : ConnectionError . leaveRequest (
975965 'Received leave request while trying to (re)connect' ,
976- ConnectionErrorReason . LeaveRequest ,
977- undefined ,
978966 firstSignalResponse . message . value . reason ,
979967 ) ,
980968 } ;
981969 } else if ( ! isReconnect ) {
982970 // non-reconnect case, should receive join response first
983971 return {
984972 isValid : false ,
985- error : new ConnectionError (
973+ error : ConnectionError . internal (
986974 `did not receive join response, got ${ firstSignalResponse . message ?. case } instead` ,
987- ConnectionErrorReason . InternalError ,
988975 ) ,
989976 } ;
990977 }
991978
992979 return {
993980 isValid : false ,
994- error : new ConnectionError ( 'Unexpected first message' , ConnectionErrorReason . InternalError ) ,
981+ error : ConnectionError . internal ( 'Unexpected first message' ) ,
995982 } ;
996983 }
997984
@@ -1010,22 +997,20 @@ export class SignalClient {
1010997 const resp = await fetch ( validateUrl ) ;
1011998 if ( resp . status . toFixed ( 0 ) . startsWith ( '4' ) ) {
1012999 const msg = await resp . text ( ) ;
1013- return new ConnectionError ( msg , ConnectionErrorReason . NotAllowed , resp . status ) ;
1000+ return ConnectionError . notAllowed ( msg , resp . status ) ;
10141001 } else if ( reason instanceof ConnectionError ) {
10151002 return reason ;
10161003 } else {
1017- return new ConnectionError (
1004+ return ConnectionError . internal (
10181005 `Encountered unknown websocket error during connection: ${ reason } ` ,
1019- ConnectionErrorReason . InternalError ,
1020- resp . status ,
1006+ { status : resp . status , statusText : resp . statusText } ,
10211007 ) ;
10221008 }
10231009 } catch ( e ) {
10241010 return e instanceof ConnectionError
10251011 ? e
1026- : new ConnectionError (
1012+ : ConnectionError . serverUnreachable (
10271013 e instanceof Error ? e . message : 'server was not reachable' ,
1028- ConnectionErrorReason . ServerUnreachable ,
10291014 ) ;
10301015 }
10311016 }
0 commit comments