diff --git a/.changeset/yummy-toys-hug.md b/.changeset/yummy-toys-hug.md new file mode 100644 index 00000000000..d31d331cf1d --- /dev/null +++ b/.changeset/yummy-toys-hug.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Better error messages for failed requests diff --git a/apps/wallet-ui/src/lib/assets/erc721.ts b/apps/wallet-ui/src/lib/assets/erc721.ts index 0ffabfd5411..d8c212e6b80 100644 --- a/apps/wallet-ui/src/lib/assets/erc721.ts +++ b/apps/wallet-ui/src/lib/assets/erc721.ts @@ -60,8 +60,10 @@ export async function getErc721Tokens({ ); if (!response.ok) { - response.body?.cancel(); - console.error("Failed to fetch NFTs"); + const error = await response.text().catch(() => null); + console.error( + `Failed to fetch NFTs: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); return { nextCursor: undefined, tokens: [], diff --git a/apps/wallet-ui/src/lib/chains.ts b/apps/wallet-ui/src/lib/chains.ts index e0d103f9285..e8a19417c88 100644 --- a/apps/wallet-ui/src/lib/chains.ts +++ b/apps/wallet-ui/src/lib/chains.ts @@ -9,8 +9,10 @@ export async function getChains() { ); if (!response.ok) { - response.body?.cancel(); - throw new Error("Failed to fetch chains"); + const error = await response.text().catch(() => null); + throw new Error( + `Failed to fetch chains: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } return (await response.json()).data as ChainMetadata[]; diff --git a/packages/thirdweb/src/pay/buyWithCrypto/getHistory.ts b/packages/thirdweb/src/pay/buyWithCrypto/getHistory.ts index a49a7fb7732..bb564769cc4 100644 --- a/packages/thirdweb/src/pay/buyWithCrypto/getHistory.ts +++ b/packages/thirdweb/src/pay/buyWithCrypto/getHistory.ts @@ -79,8 +79,10 @@ export async function getBuyWithCryptoHistory( // Assuming the response directly matches the SwapResponse interface if (!response.ok) { - response.body?.cancel(); - throw new Error(`HTTP error! status: ${response.status}`); + const error = await response.text().catch(() => null); + throw new Error( + `HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } const data: BuyWithCryptoHistoryData = (await response.json()).result; diff --git a/packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts b/packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts index 8f8099812e6..7c74d572039 100644 --- a/packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts +++ b/packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts @@ -142,8 +142,10 @@ export async function getBuyWithCryptoStatus( // Assuming the response directly matches the BuyWithCryptoStatus interface if (!response.ok) { - response.body?.cancel(); - throw new Error(`HTTP error! status: ${response.status}`); + const error = await response.text().catch(() => null); + throw new Error( + `HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } const data: BuyWithCryptoStatus = (await response.json()).result; diff --git a/packages/thirdweb/src/pay/buyWithFiat/getHistory.ts b/packages/thirdweb/src/pay/buyWithFiat/getHistory.ts index 32a0895a00e..a6e557b1d80 100644 --- a/packages/thirdweb/src/pay/buyWithFiat/getHistory.ts +++ b/packages/thirdweb/src/pay/buyWithFiat/getHistory.ts @@ -78,8 +78,10 @@ export async function getBuyWithFiatHistory( // Assuming the response directly matches the BuyWithFiatStatus response interface if (!response.ok) { - response.body?.cancel(); - throw new Error(`HTTP error! status: ${response.status}`); + const error = await response.text().catch(() => null); + throw new Error( + `HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } const data: BuyWithFiatHistoryData = (await response.json()).result; diff --git a/packages/thirdweb/src/pay/buyWithFiat/getStatus.ts b/packages/thirdweb/src/pay/buyWithFiat/getStatus.ts index a2609e6c806..89f95e88b24 100644 --- a/packages/thirdweb/src/pay/buyWithFiat/getStatus.ts +++ b/packages/thirdweb/src/pay/buyWithFiat/getStatus.ts @@ -188,8 +188,10 @@ export async function getBuyWithFiatStatus( const response = await getClientFetch(params.client)(url); if (!response.ok) { - response.body?.cancel(); - throw new Error(`HTTP error! status: ${response.status}`); + const error = await response.text().catch(() => null); + throw new Error( + `HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } return (await response.json()).result; diff --git a/packages/thirdweb/src/pay/getBuyHistory.ts b/packages/thirdweb/src/pay/getBuyHistory.ts index f893612af3f..dfe8b8a3c69 100644 --- a/packages/thirdweb/src/pay/getBuyHistory.ts +++ b/packages/thirdweb/src/pay/getBuyHistory.ts @@ -93,8 +93,10 @@ export async function getBuyHistory( // Assuming the response directly matches the SwapResponse interface if (!response.ok) { - response.body?.cancel(); - throw new Error(`HTTP error! status: ${response.status}`); + const error = await response.text().catch(() => null); + throw new Error( + `HTTP error! status: ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } const data: BuyHistoryData = (await response.json()).result; diff --git a/packages/thirdweb/src/rpc/fetch-rpc.ts b/packages/thirdweb/src/rpc/fetch-rpc.ts index 885d3b92a5e..115f210cc7e 100644 --- a/packages/thirdweb/src/rpc/fetch-rpc.ts +++ b/packages/thirdweb/src/rpc/fetch-rpc.ts @@ -72,9 +72,9 @@ export async function fetchRpc( }); if (!response.ok) { - response.body?.cancel(); + const error = await response.text().catch(() => null); throw new Error( - `RPC request failed with status ${response.status} - ${response.statusText}`, + `RPC request failed with status ${response.status} - ${response.statusText}: ${error || "unknown error"}`, ); } @@ -116,8 +116,10 @@ export async function fetchSingleRpc( }); if (!response.ok) { - response.body?.cancel(); - throw new Error(`RPC request failed with status ${response.status}`); + const error = await response.text().catch(() => null); + throw new Error( + `RPC request failed with status ${response.status} - ${response.statusText}: ${error || "unknown error"}`, + ); } if (response.headers.get("Content-Type")?.startsWith("application/json")) { return await response.json(); diff --git a/packages/thirdweb/src/transaction/actions/gasless/providers/biconomy.ts b/packages/thirdweb/src/transaction/actions/gasless/providers/biconomy.ts index 1d91b69429d..793a6023950 100644 --- a/packages/thirdweb/src/transaction/actions/gasless/providers/biconomy.ts +++ b/packages/thirdweb/src/transaction/actions/gasless/providers/biconomy.ts @@ -139,7 +139,6 @@ export async function relayBiconomyTransaction( }, ); if (!response.ok) { - response.body?.cancel(); throw new Error(`Failed to send transaction: ${await response.text()}`); } const json = await response.json(); diff --git a/packages/thirdweb/src/transaction/actions/gasless/providers/openzeppelin.ts b/packages/thirdweb/src/transaction/actions/gasless/providers/openzeppelin.ts index d643fd4f1f2..af5b11482d1 100644 --- a/packages/thirdweb/src/transaction/actions/gasless/providers/openzeppelin.ts +++ b/packages/thirdweb/src/transaction/actions/gasless/providers/openzeppelin.ts @@ -157,7 +157,6 @@ export async function relayOpenZeppelinTransaction( }); if (!response.ok) { - response.body?.cancel(); throw new Error(`Failed to send transaction: ${await response.text()}`); } const json = await response.json();