From c3104bc19ce4b9802ea704c46691fe669681937c Mon Sep 17 00:00:00 2001 From: lukasIO Date: Thu, 11 Jul 2024 11:19:36 +0200 Subject: [PATCH] Make sure metadata check is async (#1187) * Make sure metadata check is async * use proper error objects for rejection * more generic signal error --- src/room/errors.ts | 11 +++++++++++ src/room/participant/LocalParticipant.ts | 22 ++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/room/errors.ts b/src/room/errors.ts index 7e4cd1b595..002de0ba88 100644 --- a/src/room/errors.ts +++ b/src/room/errors.ts @@ -1,3 +1,5 @@ +import { ErrorResponse_Reason } from '@livekit/protocol'; + export class LivekitError extends Error { code: number; @@ -63,6 +65,15 @@ export class PublishDataError extends LivekitError { } } +export class SignalRequestError extends LivekitError { + reason: ErrorResponse_Reason; + + constructor(message: string, reason: ErrorResponse_Reason = ErrorResponse_Reason.UNKNOWN) { + super(15, message); + this.reason = reason; + } +} + export enum MediaDeviceFailure { // user rejected permissions PermissionDenied = 'PermissionDenied', diff --git a/src/room/participant/LocalParticipant.ts b/src/room/participant/LocalParticipant.ts index 8970cda5ec..6017ddbcd7 100644 --- a/src/room/participant/LocalParticipant.ts +++ b/src/room/participant/LocalParticipant.ts @@ -15,7 +15,13 @@ import type { InternalRoomOptions } from '../../options'; import { PCTransportState } from '../PCTransportManager'; import type RTCEngine from '../RTCEngine'; import { defaultVideoCodec } from '../defaults'; -import { DeviceUnsupportedError, TrackInvalidError, UnexpectedConnectionState } from '../errors'; +import { + DeviceUnsupportedError, + LivekitError, + SignalRequestError, + TrackInvalidError, + UnexpectedConnectionState, +} from '../errors'; import { EngineEvent, ParticipantEvent, TrackEvent } from '../events'; import LocalAudioTrack from '../track/LocalAudioTrack'; import LocalTrack from '../track/LocalTrack'; @@ -98,7 +104,7 @@ export default class LocalParticipant extends Participant { number, { resolve: (arg: any) => void; - reject: (reason: any) => void; + reject: (reason: LivekitError) => void; values: Partial>; } >; @@ -198,7 +204,7 @@ export default class LocalParticipant extends Participant { const { requestId, reason, message } = error; const failedRequest = this.pendingSignalRequests.get(requestId); if (failedRequest) { - failedRequest.reject({ reason, message }); + failedRequest.reject(new SignalRequestError(message, reason)); this.pendingSignalRequests.delete(requestId); } }; @@ -253,8 +259,8 @@ export default class LocalParticipant extends Participant { const startTime = performance.now(); this.pendingSignalRequests.set(requestId, { resolve, - reject: (reason: any) => { - reject(reason); + reject: (error: LivekitError) => { + reject(error); isRejected = true; }, values: { name, metadata, attributes }, @@ -270,11 +276,11 @@ export default class LocalParticipant extends Participant { resolve(); return; } - sleep(50); + await sleep(50); } - reject({ reason: 'TIMEOUT', message: 'Request to update local metadata timed out' }); + reject(new SignalRequestError('Request to update local metadata timed out')); } catch (e: any) { - if (e instanceof Error) reject({ reason: e.name, message: e.message }); + if (e instanceof Error) reject(e); } }); }