From 71ef992c6082fe5336ae6c41de2c44903eb69fbb Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Bajpai Date: Fri, 31 Jul 2026 00:46:51 +0530 Subject: [PATCH] fix: improve error handling --- examples/1-basic-usage.js | 3 ++- examples/2-multiple-providers.js | 12 ++++++++---- examples/8-cross-provider-fallback.js | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/examples/1-basic-usage.js b/examples/1-basic-usage.js index 6a6759d..def48a8 100644 --- a/examples/1-basic-usage.js +++ b/examples/1-basic-usage.js @@ -38,7 +38,8 @@ async function main() { }) }); - const data = await response.json(); + if (!response.ok) throw new Error("Request failed"); +const data = await response.json(); console.log("✅ Response:", data.choices[0].message.content); // Check budget status diff --git a/examples/2-multiple-providers.js b/examples/2-multiple-providers.js index 5f63162..cc11716 100644 --- a/examples/2-multiple-providers.js +++ b/examples/2-multiple-providers.js @@ -32,7 +32,8 @@ async function callOpenAI() { }) }); - const data = await response.json(); + if (!response.ok) throw new Error("Request failed"); +const data = await response.json(); console.log(" Response:", data.choices[0].message.content); } @@ -53,7 +54,8 @@ async function callAnthropic() { }) }); - const data = await response.json(); + if (!response.ok) throw new Error("Request failed"); +const data = await response.json(); console.log(" Response:", data.content[0].text); } @@ -71,7 +73,8 @@ async function callGemini() { } ); - const data = await response.json(); + if (!response.ok) throw new Error("Request failed"); +const data = await response.json(); console.log(" Response:", data.candidates[0].content.parts[0].text); } @@ -90,7 +93,8 @@ async function callGrok() { }) }); - const data = await response.json(); + if (!response.ok) throw new Error("Request failed"); +const data = await response.json(); console.log(" Response:", data.choices[0].message.content); } diff --git a/examples/8-cross-provider-fallback.js b/examples/8-cross-provider-fallback.js index fa5a451..9afa3da 100644 --- a/examples/8-cross-provider-fallback.js +++ b/examples/8-cross-provider-fallback.js @@ -68,7 +68,8 @@ async function main() { }), }); - const data = await response.json(); + if (!response.ok) throw new Error("Request failed"); +const data = await response.json(); console.log("\nResponse:", data.choices?.[0]?.message?.content); // Budget is tracked across all providers