Skip to content

Commit 2a7df33

Browse files
[SDK] Improve error messages for failed HTTP requests (#6734)
1 parent 348e3ab commit 2a7df33

File tree

11 files changed

+39
-20
lines changed

11 files changed

+39
-20
lines changed

.changeset/yummy-toys-hug.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Better error messages for failed requests

apps/wallet-ui/src/lib/assets/erc721.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export async function getErc721Tokens({
6060
);
6161

6262
if (!response.ok) {
63-
response.body?.cancel();
64-
console.error("Failed to fetch NFTs");
63+
const error = await response.text().catch(() => null);
64+
console.error(
65+
`Failed to fetch NFTs: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
66+
);
6567
return {
6668
nextCursor: undefined,
6769
tokens: [],

apps/wallet-ui/src/lib/chains.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ export async function getChains() {
99
);
1010

1111
if (!response.ok) {
12-
response.body?.cancel();
13-
throw new Error("Failed to fetch chains");
12+
const error = await response.text().catch(() => null);
13+
throw new Error(
14+
`Failed to fetch chains: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
15+
);
1416
}
1517

1618
return (await response.json()).data as ChainMetadata[];

packages/thirdweb/src/pay/buyWithCrypto/getHistory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,10 @@ export async function getBuyWithCryptoHistory(
7979

8080
// Assuming the response directly matches the SwapResponse interface
8181
if (!response.ok) {
82-
response.body?.cancel();
83-
throw new Error(`HTTP error! status: ${response.status}`);
82+
const error = await response.text().catch(() => null);
83+
throw new Error(
84+
`HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
85+
);
8486
}
8587

8688
const data: BuyWithCryptoHistoryData = (await response.json()).result;

packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ export async function getBuyWithCryptoStatus(
142142

143143
// Assuming the response directly matches the BuyWithCryptoStatus interface
144144
if (!response.ok) {
145-
response.body?.cancel();
146-
throw new Error(`HTTP error! status: ${response.status}`);
145+
const error = await response.text().catch(() => null);
146+
throw new Error(
147+
`HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
148+
);
147149
}
148150

149151
const data: BuyWithCryptoStatus = (await response.json()).result;

packages/thirdweb/src/pay/buyWithFiat/getHistory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ export async function getBuyWithFiatHistory(
7878

7979
// Assuming the response directly matches the BuyWithFiatStatus response interface
8080
if (!response.ok) {
81-
response.body?.cancel();
82-
throw new Error(`HTTP error! status: ${response.status}`);
81+
const error = await response.text().catch(() => null);
82+
throw new Error(
83+
`HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
84+
);
8385
}
8486

8587
const data: BuyWithFiatHistoryData = (await response.json()).result;

packages/thirdweb/src/pay/buyWithFiat/getStatus.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,10 @@ export async function getBuyWithFiatStatus(
188188
const response = await getClientFetch(params.client)(url);
189189

190190
if (!response.ok) {
191-
response.body?.cancel();
192-
throw new Error(`HTTP error! status: ${response.status}`);
191+
const error = await response.text().catch(() => null);
192+
throw new Error(
193+
`HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
194+
);
193195
}
194196

195197
return (await response.json()).result;

packages/thirdweb/src/pay/getBuyHistory.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ export async function getBuyHistory(
9393

9494
// Assuming the response directly matches the SwapResponse interface
9595
if (!response.ok) {
96-
response.body?.cancel();
97-
throw new Error(`HTTP error! status: ${response.status}`);
96+
const error = await response.text().catch(() => null);
97+
throw new Error(
98+
`HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
99+
);
98100
}
99101

100102
const data: BuyHistoryData = (await response.json()).result;

packages/thirdweb/src/rpc/fetch-rpc.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ export async function fetchRpc(
7272
});
7373

7474
if (!response.ok) {
75-
response.body?.cancel();
75+
const error = await response.text().catch(() => null);
7676
throw new Error(
77-
`RPC request failed with status ${response.status} - ${response.statusText}`,
77+
`RPC request failed with status ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
7878
);
7979
}
8080

@@ -116,8 +116,10 @@ export async function fetchSingleRpc(
116116
});
117117

118118
if (!response.ok) {
119-
response.body?.cancel();
120-
throw new Error(`RPC request failed with status ${response.status}`);
119+
const error = await response.text().catch(() => null);
120+
throw new Error(
121+
`RPC request failed with status ${response.status} - ${response.statusText}: ${error || "unknown error"}`,
122+
);
121123
}
122124
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
123125
return await response.json();

packages/thirdweb/src/transaction/actions/gasless/providers/biconomy.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ export async function relayBiconomyTransaction(
139139
},
140140
);
141141
if (!response.ok) {
142-
response.body?.cancel();
143142
throw new Error(`Failed to send transaction: ${await response.text()}`);
144143
}
145144
const json = await response.json();

packages/thirdweb/src/transaction/actions/gasless/providers/openzeppelin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ export async function relayOpenZeppelinTransaction(
157157
});
158158

159159
if (!response.ok) {
160-
response.body?.cancel();
161160
throw new Error(`Failed to send transaction: ${await response.text()}`);
162161
}
163162
const json = await response.json();

0 commit comments

Comments
 (0)