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