Skip to content

Commit

Permalink
Set names of errors and reasonName for enums (#1385)
Browse files Browse the repository at this point in the history
* Set names of errors and reasonName for enums

* Create smart-mayflies-pay.md
  • Loading branch information
lukasIO authored Jan 25, 2025
1 parent 445a42e commit 2813235
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/smart-mayflies-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": minor
---

Populate name property of LiveKit errors and add reasonName for enums
16 changes: 15 additions & 1 deletion src/room/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export class LivekitError extends Error {

constructor(code: number, message?: string) {
super(message || 'an error has occured');
this.name = 'LiveKitError';
this.code = code;
}
}

export const enum ConnectionErrorReason {
export enum ConnectionErrorReason {
NotAllowed,
ServerUnreachable,
InternalError,
Expand All @@ -24,52 +25,62 @@ export class ConnectionError extends LivekitError {

reason: ConnectionErrorReason;

reasonName: string;

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

export class DeviceUnsupportedError extends LivekitError {
constructor(message?: string) {
super(21, message ?? 'device is unsupported');
this.name = 'DeviceUnsupportedError';
}
}

export class TrackInvalidError extends LivekitError {
constructor(message?: string) {
super(20, message ?? 'track is invalid');
this.name = 'TrackInvalidError';
}
}

export class UnsupportedServer extends LivekitError {
constructor(message?: string) {
super(10, message ?? 'unsupported server');
this.name = 'UnsupportedServer';
}
}

export class UnexpectedConnectionState extends LivekitError {
constructor(message?: string) {
super(12, message ?? 'unexpected connection state');
this.name = 'UnexpectedConnectionState';
}
}

export class NegotiationError extends LivekitError {
constructor(message?: string) {
super(13, message ?? 'unable to negotiate');
this.name = 'NegotiationError';
}
}

export class PublishDataError extends LivekitError {
constructor(message?: string) {
super(14, message ?? 'unable to publish data');
this.name = 'PublishDataError';
}
}

Expand All @@ -80,9 +91,12 @@ export type RequestErrorReason =
export class SignalRequestError extends LivekitError {
reason: RequestErrorReason;

reasonName: string;

constructor(message: string, reason: RequestErrorReason) {
super(15, message);
this.reason = reason;
this.reasonName = typeof reason === 'string' ? reason : RequestResponse_Reason[reason];
}
}

Expand Down

0 comments on commit 2813235

Please sign in to comment.