Skip to content

Commit a057dd0

Browse files
author
Denis Gursky
authored
Merge pull request #30 from RelationalAI/dg-error-message
Better error message when failing to read server response
2 parents 42f671a + e9dcf91 commit a057dd0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@relationalai/rai-sdk-javascript",
33
"description": "RelationalAI SDK for JavaScript",
4-
"version": "0.5.10",
4+
"version": "0.5.11",
55
"author": {
66
"name": "RelationalAI",
77
"url": "https://relational.ai"

src/rest.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ export async function request<T>(url: string, options: RequestOptions = {}) {
8181
try {
8282
response = await fetch(fullUrl, opts);
8383
} catch (error: any) {
84-
if (error.message.toLowerCase().includes('failed to fetch')) {
84+
const errorMsg = error.message.toLowerCase();
85+
86+
if (
87+
errorMsg.includes('failed to fetch') || // Chrome
88+
errorMsg.includes('networkerror when attempting to fetch resource') // Firefox
89+
) {
8590
throw new Error(
8691
'Request failed due to a connectivity issue. Please check your network connection.',
8792
);
@@ -95,12 +100,19 @@ export async function request<T>(url: string, options: RequestOptions = {}) {
95100

96101
const responseClone = response.clone();
97102

98-
if (contentType && contentType.includes('application/json')) {
99-
responseBody = await response.json();
100-
} else if (contentType?.includes('multipart/form-data') && response.body) {
101-
responseBody = await parseMultipart(response);
102-
} else {
103-
responseBody = await response.text();
103+
try {
104+
if (contentType && contentType.includes('application/json')) {
105+
responseBody = await response.json();
106+
} else if (contentType?.includes('multipart/form-data') && response.body) {
107+
responseBody = await parseMultipart(response);
108+
} else {
109+
responseBody = await response.text();
110+
}
111+
} catch (error: any) {
112+
const err = new Error('Failed to read server response.');
113+
(err as any).cause = error;
114+
115+
throw err;
104116
}
105117

106118
if (options.onResponse) {

0 commit comments

Comments
 (0)