Skip to content

Commit

Permalink
Forward disconnect reason on leave requests and ConnectionErrors (#1323)
Browse files Browse the repository at this point in the history
* Forward disconnect reason on leave requests

* util

* revert pending publish await change

* Create wicked-frogs-beg.md

* Update wicked-frogs-beg.md
  • Loading branch information
lukasIO authored Nov 21, 2024
1 parent 2e497b6 commit 8c36d08
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-frogs-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Forward disconnect reason on leave requests and ConnectionErrors
2 changes: 2 additions & 0 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ export class SignalClient {
new ConnectionError(
'Received leave request while trying to (re)connect',
ConnectionErrorReason.LeaveRequest,
undefined,
resp.message.value.reason,
),
);
} else if (!opts.reconnect) {
Expand Down
12 changes: 10 additions & 2 deletions src/room/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
createDummyVideoStreamTrack,
extractChatMessage,
extractTranscriptionSegments,
getDisconnectReasonFromConnectionError,
getEmptyAudioStreamTrack,
isBrowserSupported,
isCloud,
Expand Down Expand Up @@ -562,11 +563,18 @@ class Room extends (EventEmitter as new () => TypedEmitter<RoomEventCallbacks>)
this.recreateEngine();
await connectFn(resolve, reject, nextUrl);
} else {
this.handleDisconnect(this.options.stopLocalTrackOnUnpublish);
this.handleDisconnect(
this.options.stopLocalTrackOnUnpublish,
getDisconnectReasonFromConnectionError(e),
);
reject(e);
}
} else {
this.handleDisconnect(this.options.stopLocalTrackOnUnpublish);
let disconnectReason = DisconnectReason.UNKNOWN_REASON;
if (e instanceof ConnectionError) {
disconnectReason = getDisconnectReasonFromConnectionError(e);
}
this.handleDisconnect(this.options.stopLocalTrackOnUnpublish, disconnectReason);
reject(e);
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/room/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestResponse_Reason } from '@livekit/protocol';
import { DisconnectReason, RequestResponse_Reason } from '@livekit/protocol';

export class LivekitError extends Error {
code: number;
Expand All @@ -20,12 +20,20 @@ export const enum ConnectionErrorReason {
export class ConnectionError extends LivekitError {
status?: number;

context?: unknown | DisconnectReason;

reason: ConnectionErrorReason;

constructor(message: string, reason: ConnectionErrorReason, status?: number) {
constructor(
message: string,
reason: ConnectionErrorReason,
status?: number,
context?: unknown | DisconnectReason,
) {
super(1, message);
this.status = status;
this.reason = reason;
this.context = context;
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/room/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
ChatMessage as ChatMessageModel,
ClientInfo,
ClientInfo_SDK,
DisconnectReason,
Transcription as TranscriptionModel,
} from '@livekit/protocol';
import { getBrowser } from '../utils/browserParser';
import { protocolVersion, version } from '../version';
import { type ConnectionError, ConnectionErrorReason } from './errors';
import CriticalTimers from './timers';
import type LocalAudioTrack from './track/LocalAudioTrack';
import type RemoteAudioTrack from './track/RemoteAudioTrack';
Expand Down Expand Up @@ -531,3 +533,18 @@ export function extractChatMessage(msg: ChatMessageModel): ChatMessage {
message,
};
}

export function getDisconnectReasonFromConnectionError(e: ConnectionError) {
switch (e.reason) {
case ConnectionErrorReason.LeaveRequest:
return e.context as DisconnectReason;
case ConnectionErrorReason.Cancelled:
return DisconnectReason.CLIENT_INITIATED;
case ConnectionErrorReason.NotAllowed:
return DisconnectReason.USER_REJECTED;
case ConnectionErrorReason.ServerUnreachable:
return DisconnectReason.JOIN_FAILURE;
default:
return DisconnectReason.UNKNOWN_REASON;
}
}

0 comments on commit 8c36d08

Please sign in to comment.