Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward disconnect reason on leave requests and ConnectionErrors #1323

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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;
}
}
Loading