Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Jan 24, 2025
1 parent 48c2e2b commit 4f96423
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/livekit-server-sdk/src/TwirpRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export interface Rpc {

export class TwirpError extends Error {
status: number;
code?: string;

constructor(status: number, name: string, message: string) {
constructor(name: string, message: string, status: number, code?: string) {
super(message);
this.name = name;
this.status = status;
this.code = code;
}
}

Expand Down Expand Up @@ -59,15 +61,15 @@ export class TwirpRpc {
if (!response.ok) {
const isJson = response.headers.get('content-type') === 'application/json';
let errorMessage = 'Unknown internal error';
let errorName = response.statusText;
let errorCode: string | undefined = undefined;
try {
if (isJson) {
const parsedError = (await response.json()) as Record<string, unknown>;
if ('msg' in parsedError) {
errorMessage = <string>parsedError.msg;
}
if ('code' in parsedError) {
errorName = <string>parsedError.code;
errorCode = <string>parsedError.code;
}
} else {
errorMessage = await response.text();
Expand All @@ -77,7 +79,7 @@ export class TwirpRpc {
console.debug(`Error when trying to parse error message, using defaults`, e);
}

throw new TwirpError(response.status, errorName, errorMessage);
throw new TwirpError(response.statusText, errorMessage, response.status, errorCode);
}
const parsedResp = (await response.json()) as Record<string, unknown>;

Expand Down

0 comments on commit 4f96423

Please sign in to comment.