Skip to content

Commit a08f53a

Browse files
authored
Merge pull request #1050 from appwrite/fix-already-read
fix: appwrite exception already read
2 parents 9e89b0b + 0e72ba9 commit a08f53a

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

templates/node/src/client.ts.twig

+8-5
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ class Client {
263263
const { uri, options } = this.prepareRequest(method, url, headers, params);
264264

265265
let data: any = null;
266-
let text: string = '';
267266

268267
const response = await fetch(uri, options);
269268

@@ -274,18 +273,22 @@ class Client {
274273

275274
if (response.headers.get('content-type')?.includes('application/json')) {
276275
data = await response.json();
277-
text = JSON.stringify(data);
278276
} else if (responseType === 'arrayBuffer') {
279277
data = await response.arrayBuffer();
280278
} else {
281-
text = await response.text();
282279
data = {
283-
message: text
280+
message: await response.text()
284281
};
285282
}
286283

287284
if (400 <= response.status) {
288-
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, text);
285+
let responseText = '';
286+
if (response.headers.get('content-type')?.includes('application/json') || responseType === 'arrayBuffer') {
287+
responseText = JSON.stringify(data);
288+
} else {
289+
responseText = data?.message;
290+
}
291+
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseText);
289292
}
290293

291294
return data;

templates/react-native/src/client.ts.twig

+9-3
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ class Client {
414414

415415
try {
416416
let data = null;
417+
417418
const response = await fetch(url.toString(), options);
418-
const text = await response.text()
419419

420420
const warnings = response.headers.get('x-{{ spec.title | lower }}-warning');
421421
if (warnings) {
@@ -426,12 +426,18 @@ class Client {
426426
data = await response.json();
427427
} else {
428428
data = {
429-
message: text
429+
message: await response.text()
430430
};
431431
}
432432

433433
if (400 <= response.status) {
434-
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, text);
434+
let responseText = '';
435+
if (response.headers.get('content-type')?.includes('application/json') || responseType === 'arrayBuffer') {
436+
responseText = JSON.stringify(data);
437+
} else {
438+
responseText = data?.message;
439+
}
440+
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseText);
435441
}
436442

437443
const cookieFallback = response.headers.get('X-Fallback-Cookies');

templates/web/src/client.ts.twig

+8-5
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,6 @@ class Client {
669669
const { uri, options } = this.prepareRequest(method, url, headers, params);
670670

671671
let data: any = null;
672-
let text: string = '';
673672

674673
const response = await fetch(uri, options);
675674

@@ -680,18 +679,22 @@ class Client {
680679

681680
if (response.headers.get('content-type')?.includes('application/json')) {
682681
data = await response.json();
683-
text = JSON.stringify(data);
684682
} else if (responseType === 'arrayBuffer') {
685683
data = await response.arrayBuffer();
686684
} else {
687-
text = await response.text();
688685
data = {
689-
message: text
686+
message: await response.text()
690687
};
691688
}
692689

693690
if (400 <= response.status) {
694-
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, text);
691+
let responseText = '';
692+
if (response.headers.get('content-type')?.includes('application/json') || responseType === 'arrayBuffer') {
693+
responseText = JSON.stringify(data);
694+
} else {
695+
responseText = data?.message;
696+
}
697+
throw new {{spec.title | caseUcfirst}}Exception(data?.message, response.status, data?.type, responseText);
695698
}
696699

697700
const cookieFallback = response.headers.get('X-Fallback-Cookies');

0 commit comments

Comments
 (0)