From ed22836621b7449f473d2e015e8dd4c83fd7f4b3 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Fri, 15 Dec 2023 11:48:33 +0100 Subject: [PATCH 1/4] Reject bad request response --- src/TwirpRPC.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TwirpRPC.ts b/src/TwirpRPC.ts index 9f1b232f..fef1c97e 100644 --- a/src/TwirpRPC.ts +++ b/src/TwirpRPC.ts @@ -42,7 +42,11 @@ export class TwirpRpc { body: JSON.stringify(data), }) .then(async (res) => { - resolve(camelcaseKeys(await res.json(), { deep: true })); + if (res.ok) { + resolve(camelcaseKeys(await res.json(), { deep: true })); + } else { + reject(res.statusText); + } }) .catch(reject); }); From 1ab7a18d83b6847c2cad8c11b7e7b498267538ee Mon Sep 17 00:00:00 2001 From: lukasIO Date: Fri, 15 Dec 2023 11:57:44 +0100 Subject: [PATCH 2/4] refactor to async await --- src/TwirpRPC.ts | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/TwirpRPC.ts b/src/TwirpRPC.ts index fef1c97e..7fc3f7ff 100644 --- a/src/TwirpRPC.ts +++ b/src/TwirpRPC.ts @@ -29,26 +29,23 @@ export class TwirpRpc { this.prefix = prefix || defaultPrefix; } - request(service: string, method: string, data: any, headers?: any): Promise { + async request(service: string, method: string, data: any, headers?: any): Promise { const path = `${this.prefix}/${this.pkg}.${service}/${method}`; const url = new URL(path, this.host); - return new Promise((resolve, reject) => { - fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json;charset=UTF-8', - ...headers, - }, - body: JSON.stringify(data), - }) - .then(async (res) => { - if (res.ok) { - resolve(camelcaseKeys(await res.json(), { deep: true })); - } else { - reject(res.statusText); - } - }) - .catch(reject); + + const response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=UTF-8', + ...headers, + }, + body: JSON.stringify(data), }); + + if (!response.ok) { + throw new Error(response.statusText); + } + const parsedResp = await response.json(); + return camelcaseKeys(parsedResp, { deep: true }); } } From bf722246d22576e38810eaa592d23312f4c22ef7 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Fri, 15 Dec 2023 12:01:47 +0100 Subject: [PATCH 3/4] Create clever-emus-remember.md --- .changeset/clever-emus-remember.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/clever-emus-remember.md diff --git a/.changeset/clever-emus-remember.md b/.changeset/clever-emus-remember.md new file mode 100644 index 00000000..3e346f5b --- /dev/null +++ b/.changeset/clever-emus-remember.md @@ -0,0 +1,5 @@ +--- +"livekit-server-sdk": patch +--- + +Throw error on bad Twirp response status and use async/await instead of promise chaining for improved error catching From 4644c1f71d15c8d891a8d2f449d8626c3b2c1dca Mon Sep 17 00:00:00 2001 From: lukasIO Date: Wed, 20 Dec 2023 14:04:02 +0100 Subject: [PATCH 4/4] More explicit status error --- src/TwirpRPC.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TwirpRPC.ts b/src/TwirpRPC.ts index 7fc3f7ff..0efc150e 100644 --- a/src/TwirpRPC.ts +++ b/src/TwirpRPC.ts @@ -43,7 +43,7 @@ export class TwirpRpc { }); if (!response.ok) { - throw new Error(response.statusText); + throw new Error(`Request failed with status ${response.status}: ${response.statusText}`); } const parsedResp = await response.json(); return camelcaseKeys(parsedResp, { deep: true });